处理一个问题,得到了我需要的华氏度列表,但我真的不知道如何在它旁边添加另一个列表并让它做数学运算。请帮忙,谢谢。
问题:
生成两列表的程序,显示-40°F至120°F的华氏温度及其相应的摄氏温度。表中的每一行应比前一行多5华氏度。华氏温度和摄氏温度都应精确到小数点后1位。
我的尝试:
package chapter5;
public class Assignment2 {
public static void main(String[] args) {
//T(°C) = (T(°F) - 32) × 5/9
int F = 120;
int count = 0;
int C = (F - 32) * (5/9);
while (F >= -40 && F <= 120 ){
if (F % 5 == 0){
System.out.printf("%-5d",F);
count++;
}
if (count == 1){
System.out.println();
count = 0;
}
F--;
}
}
}
答案 0 :(得分:1)
/* package whatever; // don't place package name! */
package chapter5;
/* Name of the class has to be "Main" only if the class is public. */
public class Assignment2 {
public static void main(String[] args) {
//T(°C) = (T(°F) - 32) × 5/9
final String DEGREE = "\u00b0";
int F = -40;
float C = 0.0F;
while (F <= 120 ){
C = (F - 32)*(0.5556F);
System.out.printf("%d%sF \t\t\t%.1f%sC\n", F, DEGREE, C, DEGREE);
F = F + 5;
}
}
}
<强>输出强>
-40°F -40.0°C
-35°F -37.2°C
-30°F -34.4°C
-25°F -31.7°C
-20°F -28.9°C
-15°F -26.1°C
-10°F -23.3°C
-5°F -20.6°C
0°F -17.8°C
5°F -15.0°C
10°F -12.2°C
15°F -9.4°C
20°F -6.7°C
25°F -3.9°C
30°F -1.1°C
35°F 1.7°C
40°F 4.4°C
45°F 7.2°C
50°F 10.0°C
55°F 12.8°C
60°F 15.6°C
65°F 18.3°C
70°F 21.1°C
75°F 23.9°C
80°F 26.7°C
85°F 29.4°C
90°F 32.2°C
95°F 35.0°C
100°F 37.8°C
105°F 40.6°C
110°F 43.3°C
115°F 46.1°C
120°F 48.9°C
答案 1 :(得分:0)
答案 2 :(得分:0)
我使用System.out.printf
格式化eclipse控制台中的输出。
public static void main(String[] args) {
double F = -40;
for (; F < 120; F += 5) {
double C = (F - 32) * 5 / 9;
C = BigDecimal.valueOf(C).setScale(1,RoundingMode.CEILING).doubleValue();
System.out.printf("%-10s %-10s \n", F, C);
}
}
此程序将一个Farenheit值转换为celsium,在两列中打印并将farenheit值增加5并再次将其转换为celsium。
它使用java.lang.BigDecimal
将double值舍入到1位小数。这是在方法setScale(1,RoundingMode.CEILING)
答案 3 :(得分:0)
答案 4 :(得分:-1)
这个怎么样:
// Each Celsius degree is calculated in the cycle.
double CasDouble = (F - 32.) * (5./9.);
// The double value is rounded to an integer
int C = (int) CasDouble;
// The application prints the Fahrenheit and Celsius degrees
System.out.printf("%-5d\t%-5d",F,C);
所以:
package chapter5;
public class Assignment2 {
public static void main(String[] args) {
// T(°C) = (T(°F) - 32) × 5/9
int F = 120;
int count = 0;
while (F >= -40 && F <= 120) {
if (F % 5 == 0) {
// Each Celsius degree is calculated in the cycle.
double CasDouble = (F - 32.) * (5. / 9.);
// The double value is rounded to an integer
int C = (int) CasDouble;
// The application prints the Fahrenheit and Celsius degrees
System.out.printf("%-5d\t%-5d", F, C);
count++;
}
if (count == 1) {
System.out.println();
count = 0;
}
F--;
}
}
}