1、小明买了一双鞋,价值58元,买了3件衣服,每件30元,买了5个包,每个包55.8元。小明共交了500元,问还要找回多少元。用程序表达。
public static void main(String[] args) {
float shot = 58f;//鞋子单价
float clothes = 30f;//衣服单价
float bag = 55.8f;//包单价
float total = shot+clothes*3+bag*5;//所买商品总价
System.out.println(500-total);//输出找零
}
2、计算半径为 5 圆的面积并输出。提示:面积=PI * 半径的平方要求: PI 值用常量 圆周率: 3.1415926
public static void main(String[] args) {
final float PI = 3.1415926f;
System.out.println(5*5*PI);
}
3.(将华氏温度转换为摄氏温度)编写程序,从输入对话框读入float型的华氏度,将其转换为摄氏度
转换公式如下:摄氏度=(5/9)*(华氏度-32)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float r = sc.nextFloat();
System.out.println((5f/9)*(r-32));
}
4.(计算圆柱的体积)编写程序读入圆柱的半径和高,用下列公式计算圆柱的体积:
面积=半径半径π体积= 面积*高
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float PI = 3.14159f;
float r = sc.nextFloat();
float h = sc.nextFloat();
System.out.println("体积:"+(r*r*PI*h));
}
5.(计算提成费)编写一个程序,读入费用与提成率,计算提成和总费用。例如,如果使用者键入10作为费用,15%作为提成率,计算结果显示提成非为$1.5,总费用$11.5
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
float num2 = sc.nextFloat();
System.out.println("提成$"+num1*num2+"总费用$"+(num1*num2+num1));
}
6.输入整数其后两位表示美分. 例如,输入1156表示11美元56美分.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(num/100+"美元"+num%100+"美分");
}
7.编写程序读入下列信息并打印工资单:
.雇员的名字(smith)
.每周工时数(10)
.每小时工资(6.75)
.联邦税率(20%)
.州税率(9%)
使用控制台输入和输出
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.next();
float worktime = sc.nextFloat();
float sal = sc.nextFloat();
float nationSar = sc.nextFloat();
float priveSar = sc.nextFloat();
float total = worktime*sal*(1-nationSar)*(1-priveSar);
System.out.println(total);
}
8.编写程序,读入三角形的三条边并确定输入是否有效。如果任意两条边的和大于第三条边则输入正确。
例如:输入三条边是1,2,1 输出应该是:
can edges 1,2 and 1 form a triangle? false
输入三条边是1,2,2 输出应该是:
can edges 1,2 and 2 form a triangle? true
public static void main(String[] args) {
arry(1,2,1);
arry(1,2,2);
}
private static void arry(int i, int j, int k) {
if(i+j>k&&i+k>j&&j+k>i){
System.out.println("can edges "+i+","+j+" and 1 form a triangle?true");
}else{
System.out.println("can edges "+i+","+j+" and 1 form a triangle?false");
}
}
9.编写程序,提示用户输入一个整数,而后检测该整数是否能被5和6同时整除,至少能被5或6整除,仅能被5或6整
除。下面是输入10时的输出样例
IS 10 divisible by 5 and 6? false
is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6,but not both? true
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("IS 10 divisible by 5 and 6? "+((num%5==0)&&(num%6==0)));
System.out.println("IS 10 divisible by 5 and 6? "+((num%5==0)||(num%6==0)));
System.out.println("IS 10 divisible by 5 and 6? "+((num%5==0)^(num%6==0)));
}