练习1:
使用公式C=(5/9)(F-32)打印下列华氏温度与摄氏温度对照表。
0 -17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148
思考:
加入制表符使数据输出更整齐;
让摄氏温度保留一位小数。
修改温度转换程序,要求以逆序(从300度到0度的顺序)打印温度转换表。
public class one {
public static void main(String[] args) {
double c;
int f;
for(f=300;f>=0;f=f-20){
c=(double)5/9*(f-32);
System.out.printf(f+"\t");
System.out.printf("%.1f",c);
System.out.printf("\n");
}
}
}
练习2:
打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153 是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
public class two {
public static void main(String[] args) {
System.out.println("1000以内的水仙花数:");
int n,a,b,c;
for(n=100;n<=999;n++){
a=n/100;
b=n/10%10;
c=n%10;
if((a*a*a+b*b*b+c*c*c)==n)
System.out.print(n+"\t");
}
}
}
练习3:
求1+2!+3!+...+20!的和
public class three {
public static void main(String[] args) {
int sum,n,t;
sum=0;
t=1;
for(n=1;n<=20;n++){
t=n*t;
sum=sum+t;
}
System.out.print(sum);
}
}