Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法识别Java中的变量_Java_If Statement_Variables_User Input_Command Line Arguments - Fatal编程技术网

无法识别Java中的变量

无法识别Java中的变量,java,if-statement,variables,user-input,command-line-arguments,Java,If Statement,Variables,User Input,Command Line Arguments,对于作业,我正在创建一个命令行程序,将输入的温度从摄氏度(C)更改为华氏度(F),反之亦然。程序运行正常,直到用户输入临时类型(C/F),然后它似乎无法识别用户输入。我做错了什么 public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter the temperature:");

对于作业,我正在创建一个命令行程序,将输入的温度从摄氏度(C)更改为华氏度(F),反之亦然。程序运行正常,直到用户输入临时类型(C/F),然后它似乎无法识别用户输入。我做错了什么

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the temperature:"); //Prompts user for temperature
        String temp = input.nextLine(); //Allows user to input temp data
        double tempDouble = Double.parseDouble(temp); //Changes input from string to double

        System.out.println("Is " + temp + " degrees in Celsius or Fahrenheit? (Enter C or F):"); //Prompts user for type of temp
        String type = input.nextLine(); //Allows user to input temp type

        if (type == "C") { //Checks if temp is Celsius
            double tempF = 0;
            tempF = (tempDouble * 1.8) + 32; //Converts temp to Fahrenheit
            System.out.println(tempDouble + "C equals " + tempF + "F."); //Displays conversion of C to F
            //Tf = Tc * 1.8 + 32

        } else if (type == "F") { //Checks if temp is Fahrenheit
            double tempC = 0;
            tempC = (tempDouble - 32) / 1.8; //Converts temp to Celsius
            System.out.println(tempDouble + "F equals " + tempC + "C.");
            //Tc = (Tf - 32) / 1.8
        }
        System.out.println("Incorrect input for Celsius or Fahrenheit"); //Tells user they didn't input C or F correctly
    }

代码中有两个问题

  • 您正在检查对象的相等性,而不是对象的值。用户String.equals方法
  • if-else块的构造错误,它将始终打印“不正确的摄氏或华氏输入”
  • 这是正确的-

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
    
        System.out.println("Please enter the temperature:"); //Prompts user for temperature
        String temp = input.nextLine(); //Allows user to input temp data
        double tempDouble = Double.parseDouble(temp); //Changes input from string to double
    
        System.out.println("Is " + temp + " degrees in Celsius or Fahrenheit? (Enter C or F):"); //Prompts user for type of temp
        String type = input.nextLine(); //Allows user to input temp type
    
        if ("C".equals(type)) { //Checks if temp is Celsius
            double tempF = 0;
            tempF = (tempDouble * 1.8) + 32; //Converts temp to Fahrenheit
            System.out.println(tempDouble + "C equals " + tempF + "F."); //Displays conversion of C to F
            //Tf = Tc * 1.8 + 32
    
        } else if ("F".equals(type)) { //Checks if temp is Fahrenheit
            double tempC = 0;
            tempC = (tempDouble - 32) / 1.8; //Converts temp to Celsius
            System.out.println(tempDouble + "F equals " + tempC + "C.");
            //Tc = (Tf - 32) / 1.8
        }else{
            System.out.println("Incorrect input for Celsius or Fahrenheit"); //Tells user they didn't input C or F correctly 
        }
    }
    
    您正在执行``if(type==“C”)```,这将比较对象实例,执行“C”。等于(type)