有人能告诉我为什么我的代码运行得太早了吗?

时间:2018-05-14 21:52:43

标签: java if-statement

这是一个课程,我无法弄清楚为什么我的代码在第一个问题后停止运行。它将询问用户第一个问题,允许用户输入然后说操作完成。我究竟做错了什么?谢谢。

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.");
         }     
 }
} 

1 个答案:

答案 0 :(得分:0)

你会期待什么? 您的应用中没有循环。 使用whilefordo..while循环