这是一个课程,我无法弄清楚为什么我的代码在第一个问题后停止运行。它将询问用户第一个问题,允许用户输入然后说操作完成。我究竟做错了什么?谢谢。
import java.util.Scanner; 公共类TemperatureConverter {
public static void main(String[] args) {
//Create scanner object
Scanner keyboard = new Scanner(System.in);
//Declare variables
double temperature;
double temp;
String temperatureScale;
//Use scanner to ask for user input
System.out.println("What is the temperature?");
temperature = keyboard.nextDouble();
//Use scanner to ask for user input
System.out.println("Is the temperature Celsius or Fahrenheit?");
temperatureScale = keyboard.nextLine();
//If statement
if (temperatureScale == "Fahrenheit"){
temp = temperature - 32 * 5/9;
System.out.println("The result is " + temp + "degrees Celsius.");
}
else if (temperatureScale == "Celsius"){
temp = temperature * 1.8 + 32.0;
System.out.println("The result is " + temp + "degrees
Fahrenheit.");
}
else{
System.out.println("An incorrect value was input.");
}
}
}
答案 0 :(得分:0)
你会期待什么?
您的应用中没有循环。
使用while
,for
,do..while
循环