我刚刚学习 Java,已经完全被这段代码难住了。我环顾四周,但语言知识的一部分是知道要问的正确问题,而且我已经找不到要搜索的想法了。在我看来一切都很好,但我希望有经验的人能够在这方面对我进行一些教育。
每当我在 100 到 213(无论摄氏或华氏温度)的温度范围内运行或逐步运行此代码时,Else If 将始终运行。谁能告诉我为什么会这样,或者让我进入正确的心态?这是代码:
package chapter.pkg3;
import java.util.Scanner;
public class Chapter3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the temperature: ");
double temp = in.nextDouble();
System.out.print("Please enter the temperature measurement, C or F: ");
String measure = in.next();
if (measure.equals("C"))
{ measure = "Celsius";}
else
{measure = "Fahrenheit";}
if ((temp <= 0 && measure.equals("C")) || (temp <= 32 && !measure.equals("C")))
{
System.out.print("The temperature is " + temp + " " + measure + ". The water
is freezing!");
}
if ((temp <= 100 && measure.equals("C")) || (temp <= 212 && !measure.equals("C")))
{
System.out.print("The temperature is " + temp + " " + measure + ". The water
is liquid.");
}
if ((temp > 100 && measure.equals("C")) || (temp > 212 && !measure.equals("C")))
{
System.out.print("At " + temp + " " + measure + " the water has become gas.");
}
}
}
在此先感谢您的帮助!