摄氏度和华氏度的用户输入验证

User Input Validation for Celsius and Fahrenheit

首先,感谢所有提供帮助的人。其次,请记住我是新手(如我的代码所示哈哈)。

我只是想让它执行用户输入验证。它应该验证空气温度、单位和风速。无论我输入什么,它都告诉我单位无效。我是不是在阅读键盘输入部分做了什么奇怪的事情?
这是我的程序输出(示例用户输入以粗体显示):

  • Wind Chill calculation program.
  • Enter the air temperature followed by the unit. For example, 25 F for Fahrenheit or 25 C for Celsius.
  • Air temperature: 25 F
  • Enter the wind speed (in miles per hour).
  • Wind speed: 10
  • The unit is invalid.
  • Wind Chill not calculated: Total Errors = 1
  • Wind Chill program is completed.
  • BUILD SUCCESSFUL (total time: 53 seconds)

此外,我还没有尝试过这部分,但我想让单位不区分大小写。我会用 .toLowerCase() 或 .toUpperCase() 来做到这一点,对吗?我只是想确定我的方向是正确的。

下面的链接是作业要求和程序应该执行的示例。如果你不想(当然),你不必看它们,但我添加了它们以防万一我没有很好地解释自己。

  1. Detailed program specs
  2. Code skeleton of program specs
  3. Sample program output

这是我目前的情况:

import java.util.Scanner;

public class WindChill2
{
  public static void main(String args[])
  {
        // Variables
        Scanner keyboard = new Scanner(System.in);
        double temp,                // Temperature
               windSpeed,           // Speed of wind in miles per hour
               windChill;           // Wind chill factor
        int    errorCount = 0;      // User entry errors
        char   unit;

        // Input
        System.out.println("Wind Chill calculation program.");

        System.out.println("Enter the air temperature followed by the unit. "
                         + "For example, 25 F for Fahrenheit or 25 C for " 
                         + "Celsius.");
        System.out.print("Air temperature: ");
        temp = keyboard.nextDouble();       // Store user entry in temp
        unit = keyboard.next().charAt(0);   // Store unit (C or F)
        System.out.println("Enter the wind speed (in miles per hour).");
        System.out.print("Wind speed: ");
        windSpeed = keyboard.nextDouble();  // Store user entry in windSpeed

        // Processing  
            if (temp < -40 || temp > 50)           // Validate temperature
            {
                System.out.println("The air temperature is invalid.");
                errorCount++;
            }

            if (unit != 'C' || unit != 'F')        // Validate unit
            {
                System.out.println("The unit is invalid.");
                errorCount++;
            }

            if (windSpeed < 0 || windSpeed > 50)   // Validate wind speed
            {
                System.out.println("The wind speed is invalid.");
                errorCount++;
            }

            if (errorCount > 0)                   // Error counter
            {
                System.out.println("Wind Chill not calculated: Total Errors = "
                                   + errorCount);
            } 
            else     // Calculate wind chill factor
            {
            windChill = 91.4 - (91.4 - temp) * (.478 + .301 * 
                        Math.sqrt(windSpeed) - .02 * windSpeed);
            System.out.print("The Wind Chill factor is ");
            System.out.printf("%.1f\n", windChill);
            }

        System.out.println("Wind Chill program is completed.");
  }
}

您写的检查单位的条件有误。当 unit == 'F'unit != 'C' || unit != 'F' 会 return 为真,因为其中一个条件 (unit != 'C') 为真。如果任一表达式为真,OR 运算符 (||) 将 return 为真。

要解决这个问题,只需使用 AND 运算符 (&&),这样,如果单位不等于 C AND F,则单位无效。

因此条件应该是unit != 'C' && unit != 'F'