【问题标题】:Temperature Calculator for JavaJava的温度计算器
【发布时间】:2016-02-03 16:26:53
【问题描述】:

我在编译期间收到错误消息。它期待一个.class。我不认为它应该需要一个。我在编码方面很新,所以请原谅我的无知。我还想要一些关于如何在用户输入 C 或 F 时取消 Case 的指导,以便他们可以输入 c 或 f 而不会收到错误消息。

这是我的代码:

  import java.util.Scanner;
  import java.util.InputMismatchException;
  public class TempCALC
  {
    public static void main(String[] args) 
    {
  System.out.println("This Program will allow the user to calculate temperature.");
  calculateTemp();
}
private static void calculateTemp() {
  int F;
  int C;
  F=0;
  C=1;
  Scanner input = new Scanner (System.in);
  System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
  System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
  int option = input.nextInt();
  if (int=0) {
    System.out.println("Please enter a temperature in degrees Fahrenheit.");
    ftoc();
  } else if (int= 1) {
    System.out.println("Please enter a temperature in degrees Celsius.");
    ctof();
  } else {
    System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
  }
}
private static void ftoc() {
  Scanner input = new Scanner(System.in);
  Double celsius = input.nextDouble();
  System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
  calculatetemp();
}
private static void ctof() {
  Scanner input = new Scanner(System.in);
  Double Fahrenheit = input.nextDouble();
  System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
  calculatetemp();
}
private static void print(String string); {
  System.out.println("\n" + string);
}
  }

【问题讨论】:

标签: java class if-statement while-loop java.util.scanner


【解决方案1】:
if (int=0)

这不是有效的 Java 语法。 int 是类型而不是变量。你的意思是写:

if(option == 0)

注意==(比较)而不是=(分配)。 这是您要实现的流程:

if (option == 0) {
    System.out.println("Please enter a temperature in degrees Fahrenheit.");
    ftoc();
  } else if (option == 1) {
    System.out.println("Please enter a temperature in degrees Celsius.");
    ctof();
  } else {
    System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
  }

如果您希望用户输入fc 而不是提及学位,则需要使用:

String option = input.next();

获取String 而不是Integer

然后:

if (option.equals('F') { ... }
else if(option.equals('C') { ... }
else { ... }

如果您希望输入不区分大小写,请查看 toLowerCasetoUpperCase 并将其应用于您的需求(此处有一个答案显示其用途)。

【讨论】:

  • 好的,谢谢,但现在我收到“无法比较的类型:java.util.Scanner 和 int”错误消息。那么我需要更改我在前几行中定义的变量吗?如果需要,您会如何建议这样做?
  • 第 22 行和第 25 行又称为 if "(input == 0) {" 和 "if (input == 1) {"
  • 请注意,在我的代码中它是 option == 0 而不是 input == 0。将您的代码更改为该代码,它将起作用:)
  • 哦,好的。非常感谢你一直很有帮助!我想我可以自己完成剩下的工作,但如果我有任何麻烦,一定要告诉你!再次感谢
【解决方案2】:

int是java中的保留关键字,不能用表达式来比较,你应该像这样使用他的值:

    int option = input.nextInt();
      if (option ==0) {

不是if (int=0) {= 用于分配,而是== 用于检查相等性。

【讨论】:

    【解决方案3】:

    您首先必须检查 ifs 语句中的验证,如前面的答案所述。

    另外,我认为你的实际转换公式被交换了。

    然后,为了接收'C'或'c'

    读取String,而不是int

    String option = input.next();
    

    然后将所有输入转换小写

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

    这是完整的例子:

    public class TempCALC {
        public static void main(String[] args) {
            System.out.println("This Program will allow the user to calculate temperature.");
            calculateTemp();
        }
    
        private static void calculateTemp() {
            Scanner input = new Scanner(System.in);
            System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
            System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
            String option = input.next();
            if (option.toLowerCase().equals("f")){
                System.out.println("Please enter a temperature in degrees Fahrenheit.");
                ftoc();
            }else if (option.toLowerCase().equals("c")){
                System.out.println("Please enter a temperature in degrees Celsius.");
                ctof();
            }else{
                System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
            }
        }
    
        private static void ftoc() {
            Scanner input = new Scanner(System.in);
            Double celsius = input.nextDouble();
            System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
            calculatetemp();
        }
    
        private static void ctof() {
            Scanner input = new Scanner(System.in);
            Double Fahrenheit = input.nextDouble();
            System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
            calculatetemp();
        }
    
        private static void print(String string){
            System.out.println("\n" + string);
        }
    
        private static void calculatetemp(){
            System.out.println("\nInside calculateTemp");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2015-01-09
      • 1970-01-01
      相关资源
      最近更新 更多