使用公式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 A{
public static void main(String args[]){
int c;
for(int i=0;i<=300;i=i+20){
System.out.print(i+" ");
c=(int)((5/9.0)*(i-32));
System.out.println(c);
}
}
}
public class A1{
public static void main(String args[]){
float c;
for(int i=300;i>=0;i=i-20){
System.out.print(i+"\t");
c=(float)((5/9.0)*(i-32));
System.out.printf("%.1f",c);
System.out.println();
}
}
}
总结:注意公式C=(5/9)(F-32)的数据类型,保留一位小数时注意格式,合理使用制表符。