在Java中,变量未被识别。

huangapple go评论72阅读模式
英文:

Variable in Java Not Recognized

问题

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.equals("C")) { // Checks if temp is Celsius
        double tempF = (tempDouble * 1.8) + 32; // Converts temp to Fahrenheit
        System.out.println(tempDouble + "C equals " + tempF + "F."); // Displays conversion of C to F

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

For an assignment I'm creating a command line program to change an inputted temp from Celsius (C) to Fahrenheit (F) & vice versa. The program runs fine until the user inputs the temp type (C/F), and then it doesn't seem to recognize the user input. What I am doing wrong?

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
	}

答案1

得分: 1

以下是翻译好的内容:

代码中存在两个问题

  1. 你在检查对象相等性,而不是对象的值。应该使用 String.equals 方法进行比较。
  2. if-else 块的结构错误,它总是会打印出 "温度输入有误,不是摄氏温度或华氏温度"。

以下是正确的代码示例 -

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

    System.out.println("请输入温度:"); //提示用户输入温度
    String temp = input.nextLine(); //允许用户输入温度数据
    double tempDouble = Double.parseDouble(temp); //将输入从字符串转换为双精度

    System.out.println("是摄氏温度还是华氏温度?(输入 C 表示摄氏温度,输入 F 表示华氏温度):"); //提示用户输入温度类型
    String type = input.nextLine(); //允许用户输入温度类型

    if ("C".equals(type)) { //检查温度是否为摄氏温度
        double tempF = 0;
        tempF = (tempDouble * 1.8) + 32; //将温度转换为华氏温度
        System.out.println(tempDouble + "摄氏度等于 " + tempF + "华氏度。"); //显示摄氏温度转华氏温度的转换结果
        //Tf = Tc * 1.8 + 32

    } else if ("F".equals(type)) { //检查温度是否为华氏温度
        double tempC = 0;
        tempC = (tempDouble - 32) / 1.8; //将温度转换为摄氏温度
        System.out.println(tempDouble + "华氏度等于 " + tempC + "摄氏度。");
        //Tc = (Tf - 32) / 1.8
    } else {
        System.out.println("温度输入有误,不是摄氏温度或华氏温度"); //告诉用户他们没有正确输入 C 或 F
    }
}
英文:

There are two problems in the code

  1. You are checking object equality not the value of the object. User String.equals method for that.
  2. The construct of the if-else block is wrong, it will always print "Incorrect input for Celsius or Fahrenheit".

Here is the correct one -

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 
    }
}

huangapple
  • 本文由 发表于 2020年9月20日 00:13:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63970853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定