我在Java中的温度转换代码有什么问题?

时间:2016-04-15 00:57:57

标签: java compilation

我一直在努力做的事情:

- 询问温度

- 询问要转换为什么(“C”,“c”,“F”,“f”)

编译很好,但是当问到第二个问题时,我无法输入任何内容,这使我不能继续前进。有帮助吗?谢谢!

import java.util.Scanner;

public class TemperatureConversionSelection
{
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a temperature in degrees (for example 29.6): ");
    double temp;
    temp = keyboard.nextInt();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter;
    letter = keyboard.nextLine();

    if (letter == "F"){
       double total = (9)*(temp)/(5) + (32);
    System.out.println(temp + " degrees F = " + total + "degrees Celsius");}
    if (letter == "f"){
        double total = (9)*(temp)/(5) + (32);{
    System.out.println(temp + " degrees F = " + total + "degrees Celsius");}
    if (letter == "C"){
        double total2 = (5)*(temp - 32)/(9);
    System.out.println(temp + " degrees C = " + total + "degrees Fahrenheit");}
    if (letter == "c"){
        double total2 = (5)*(temp - 32)/(9);
    System.out.println(temp + " degrees C = " + total + "degrees Fahrenheit");}
    }
}
}

2 个答案:

答案 0 :(得分:1)

问题是Scanner.nextInt()方法不会消耗输入的最后一个换行符

您需要使用keyboard.nextLine();

消耗上一个换行符

请改为尝试:

    temp = keyboard.nextInt();

    //Consume the newline that nextInt left
    keyboard.nextLine();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter;
    letter = keyboard.nextLine();

示例运行:

  

运行:
  输入以度为单位的温度(例如29.6):
  5
  为华氏度输入'F'(或'f')或为摄氏度输入'C'(或'c'):
  ˚F
  5.0华氏度= 41.0摄氏度

结合其他事情: 您不会将字符串与==进行比较,因为这是对参考相等性的测试。您使用String.equals(String)String.compareTo(String)。 例如,您的行

if (letter == "F"){

应该

if (letter.equals("F")){

tempdouble,因此您需要替换

temp = keyboard.nextInt();

temp = keyboard.nextDouble();

你有重复的代码,你不需要2个单独的if块来检查大写和小写字母,你可以这样做:

    if (letter.equals("F") || letter.equals("f")) {

练习代码格式化,这样可以轻松捕获以下内容:
您会注意到以下代码

if (letter == "f"){
    double total = (9)*(temp)/(5) + (32);{
System.out.println(temp + " degrees F = " + total + "degrees Celsius");}

{

行上有一个额外的double total = (9)*(temp)/(5) + (32);{

这将使您的代码检查字母C / c永远不会到达。通过正确缩进并删除额外的{来修复它,如下所示:

    if (letter == "f") {
        double total = (9) * (temp) / (5) + (32);
        System.out.println(temp + " degrees F = " + total + "degrees Celsius");
    }

练习良好的代码语法,以便于阅读。例如行

   double total = (9)*(temp)/(5) + (32);
如果你这样写的话,

会更容易阅读(并且更有意义):

   double total = 9 * (temp / 5) + 32;

在这种情况下,你实际上甚至不需要括号,因为操作的顺序将处理正确的方程式,所以你可以写:

   double total = 9 * temp / 5 + 32;

较少的括号使其更容易理解。

答案 1 :(得分:0)

在Java中,字符串的比较如下:

String whatever = "hi";
if(whatever.equals("hi"){
    //do stuff
}
else{
    //do other stuff
}

只需更正if语句即可。

编辑:

我没有解决您跳过输入的问题。我不认为实际上跳过了输入。它接受输入,并尝试在if语句中进行比较。但是,因为if语句写得不正确,所以它们都返回false,然后转到程序的末尾。

EDIT2:

抱歉我的错误假设。我尝试自己运行,我得到了下面的代码:

import java.util.Scanner;

public class temp
{
public static void main(String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a temperature in degrees (for example 29.6): ");
    double temp;
    temp = keyboard.nextDouble();

    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
    String letter = keyboard.next();
    double total = 0;

    if (letter.equals("F")){
        total = (9)*(temp)/(5) + (32);
        System.out.println(temp + " degrees F = " + total + " degrees Celsius");}
    if (letter.equals("f")){
        total = (9)*(temp)/(5) + (32);
        System.out.println(temp + " degrees F = " + total + " degrees Celsius");}
    if (letter.equals("C")){
        total = (5)*(temp - 32)/(9);
        System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");}
    if (letter.equals("c")){
        total = (5)*(temp - 32)/(9);
        System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
    }
}

}