我需要将摄氏度转换为华氏度的表格。输出应该完全像这样:
onReceive()
诀窍是,整个过程只能使用一个while循环。同样,您不能仅输入所有内容。我正在努力寻找一种方法,使它在每次经过10次转换或类似类型的转换时都开始新的列,我编写了一个程序,但它的la脚和有点作弊:
-40.0C is -40.0F -30.0C is -22.0F -20.0C is -4.0F -10.0C is 14.0F
-39.0C is -38.2F -29.0C is -20.2F -19.0C is -2.2F -9.0C is 15.8F
-38.0C is -36.4F -28.0C is -18.4F -18.0C is -0.4F -8.0C is 17.6F
-37.0C is -34.6F -27.0C is -16.6F -17.0C is 1.4F -7.0C is 19.4F
-36.0C is -32.8F -26.0C is -14.8F -16.0C is 3.2F -6.0C is 21.2F
-35.0C is -31.0F -25.0C is -13.0F -15.0C is 5.0F -5.0C is 23.0F
-34.0C is -29.2F -24.0C is -11.2F -14.0C is 6.8F -4.0C is 24.8F
-33.0C is -27.4F -23.0C is -9.4F -13.0C is 8.6F -3.0C is 26.6F
-32.0C is -25.6F -22.0C is -7.6F -12.0C is 10.4F -2.0C is 28.4F
-31.0C is -23.8F -21.0C is -5.8F -11.0C is 12.2F -1.0C is 30.2F
所以如果有人有建议请解释。
答案 0 :(得分:2)
我强烈建议您创建一种方法,进行从摄氏到华氏的转换,并打印出来。在我的示例中,我将使用conv(double celcius)
打印例如“ 40.0C is -40.0F”。
有一种模式:i 0 到i 9 的十行分别包含-40 + i
,-30 + i
,{{1}的转换}和-20 + i
。
在while循环中,您必须调用-10 + i
方法四次。
conv
int i = 0;
while (i < 10) {
conv(-40 + i);
conv(-30 + i);
conv(-20 + i);
conv(-10 + i);
System.out.println();
i++;
}
答案 1 :(得分:2)
假设您不希望在while循环外使用计数器变量,并且不想在每次迭代中打印多于一对摄氏-华氏温度:
double celsius = -50.0;
double fahrenheit;
while (celsius < -1){
if(celsius >= -10){
celsius -= 39;
System.out.println();
}
celsius1 += 10;
fahreheit = (celsius * (1.8) + 32.0);
System.out.printf ("%.1fC is %.1fF\t",celsius, fahrenheit);
}
答案 2 :(得分:0)
好吧,它们彼此相差十度,因此您可以消除几个变量以简化代码:
public class TempConvWorking {
public static void main (String[] args) {
double celsius = -40.0;
while (celsius < -30.0) {
System.out.printf(
"%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t%.1fC is %.1fF\t\n",
celsius,
1.8*celsius + 32.0,
celsius + 10.0,
1.8*celsius + 50.0,
celsius + 20.0,
1.8*celsius + 68.0,
celsius + 30.0,
1.8*celsius + 86.0);
celsius++;
}
}
}