-1
package temperatureconversion;
import java.util.Scanner;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter Conversion type: Press C for Celsius to Fahrenheit or press F     For Fahrenheit to Celsius.");

 String Vctype = keyboard.next();

    if (Vctype == "f" || Vctype == "F"){
        System.out.println("Please enter fahrenheit");
        double Vfahrenheit = keyboard.nextInt();
        Vfahrenheit = (Vfahrenheit)*(9/5)+(32);
            System.out.println(Vfahrenheit);
    }
    if (Vctype == "c" || Vctype == "C"){
        System.out.println("Please enter celcius");
        double Vcelcius = keyboard.nextInt();
        Vcelcius = (Vcelcius - 32)*(5/9);
               System.out.println(Vcelcius) ;
    }        
  }   
}

大家好,我想知道是否有人可以帮助我处理上述代码。基本上在netbeans的输出控制台中,程序似乎只是在我点击C或F后结束,但它应该要求输入一个数字,然后允许输入一个数字,然后计算并最终显示计算结果。它似乎没有执行 if 语句我哪里出错了?

4

1 回答 1

0

您正在将 String 与==. 所以它不起作用,你必须使用这个:

if (Vctype.toLowerCase().equals("f"))

另请注意,使用“toLowerCase”会使整个字符串变为小写,因此您不必为“F”和“f”提供两个选项。

如果需要,可以使用“compareTo”

if (Vctype.toLowerCase().compareTo("f") == 0)
于 2013-10-12T16:34:01.307 回答