import acm.program.ConsoleProgram;
public class Name extends ConsoleProgram {
public void run() {
double converted = 0;
println("What do you want me to do? Enter 1 to convert from Celsius to Fahrenheit. Enter 2 to convert from Fahrenheit to Celsius.");
int mode = readInt("Please enter a mode: ");
if (mode == 1) {
double celsius = readDouble("Please enter a temperature: ");
converted = (celsius * 1.8 + 32);
println(converted);
while (true) {
readDouble("Please enter the temperature: ");
converted = (celsius * 1.8 + 32);
println(converted);
if (celsius <= -1337)
break;
println("Goodbye!");
}
} else if (mode == 2) {
double fahrenheit = readDouble("Please enter the temperature: ");
converted = (fahrenheit - 32) / 1.8;
println(converted);
}
}
public static void main(String[] args) {
new Name().start();
}
}
摄氏温度是一种非常合理的温度测量单位:0°C是水的冰点,100°C是水的沸点(在某些条件下)。但是,某些国家仍然认为华氏温度是更好的选择。这就是为什么在此作业中您将编写一个程序以从华氏温度转换为摄氏温度然后返回的原因。这两个单元之间的关系是这样的:C = 59(F−32)
编写一个扩展ConsoleProgram的程序TemperatureConverter。该程序首先询问用户是否将摄氏温度转换为华氏度(在这种情况下,用户需要输入1)还是将华氏温度转换为摄氏(在这种情况下用户需要输入2)。然后,程序要求输入温度并输出转换后的温度。重复此过程,直到哨兵中止循环。在我们的情况下,温度-1337作为定点值。
请注意,如果用户进入无效模式,则您的程序应仅停止产生输出。不要抛出异常(以防万一您已经知道异常是什么),也不要反复询问用户有效的模式。
这里是示例运行示例:
你想让我做什么?
((1)将摄氏温度转换为华氏温度
((2)将华氏温度转换为摄氏度
模式:2
温度:41
5.0
温度:32
0.0
温度:-1337
再见!
//有人可以帮我还是告诉我代码中要进行哪些更改?我是一个初学者。