即使我在 if 语句之外定义了它,我的程序也没有在 if 语句中找到变量(简单计算器)

My program doesn't find the variable inside the if statement even when I've defined it outside the if statement(simple calculator)

我添加了一个 if 语句,当我在第二个 if 语句块中进行计算时,将输入的数字解析为双精度数,程序必须 运行 如果只输入数字而不是字母,当我输入 double(5.5) 时,该程序似乎无法读取数字,但当我输入 int(5) number/numbers.

时,程序运行正常
Scanner numbers = new Scanner(System.in);
Scanner operation = new Scanner(System.in);

double number1 = 0;
double number2 = 0;
String operator;


System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
operator = operation.next();


System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();


System.out.print("Enter your second number: ");
String num2 = numbers.nextLine();


boolean check1 = num1.trim().matches("^[0-9]+$");
boolean check2 = num2.trim().matches("^[0-9]+$");


if (check1 == true && check2 == true){
    number1 = Double.parseDouble(num1);
    number2 = Double.parseDouble(num2);
}else {
    System.out.println("Only enter numbers not letters.");
}


String calculation;
if (operator.equals("+")){
    calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
    System.out.println(calculation);

}else if (operator.equals("-")){
    calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
    System.out.println(calculation);

}else if (operator.equals("*")){
    calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
    System.out.println(calculation);

}else if (operator.equals("/")){
    calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
    System.out.println(calculation);

}else{
    calculation = operator + ":" + " Is not a valid operator!";
    System.out.println(calculation);

}

我认为问题可能出在 (.) 上, 我输出到控制台

Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5.5
Enter your second number: 5.5
Only enter numbers not letters.
0.0 + 0.0 = 0.0 

现在 int 数字可以正常工作了。

Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5
Enter your second number: 5
5.0 + 5.0 = 10.0

正则表达式“^[0-9]+$”仅对整数求值为 true,因为它会检查字符串是否由 0 - 9 组成。如果您还想检查双精度值,这可能会有所帮助: https://www.baeldung.com/java-check-string-number

第一个建议可能是最简单的。用 try catch 包围 Double.parseDouble,因为如果它不是数字,它会给你一个 NumberFormatException。

不要使用超过一个 Scanner 对象。不需要超过一个,这也可能会产生一些您不想处理的副作用。

由于您使用的是正则表达式并且其他人提出了其他解决方案,因此我决定向您展示如何使用正则表达式计算 floating-point 值。正确的表达方式是

[-+]?[0-9]*\.?[0-9]+

或更好...

[-+]?\d*\.?\d+

此表达式计算的值可能包含一个可选的符号(正号或负号),后跟零个或多个数字,然后是可选的小数点和无限数量的小数位。但是,如果使用小数点,则必须至少有一位数字。在 +2. 这样的情况下,只有 +2 匹配。

下面的代码是您的解决方案版本,稍作修改:

public static void main(String[] args) {
    final String REGEX = "[-+]?[0-9]*\.?[0-9]+";
    Scanner scanner = new Scanner(System.in);

    double number1 = 0;
    double number2 = 0;
    String operator;
    
    System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
    operator = scanner.next();

    System.out.print("Enter the first number: ");
    String num1 = scanner.next();

    System.out.print("Enter your second number: ");
    String num2 = scanner.next();
    scanner.close();

    boolean check1 = num1.trim().matches(REGEX);
    boolean check2 = num2.trim().matches(REGEX);

    if (check1 && check2) {
        number1 = Double.parseDouble(num1);
        number2 = Double.parseDouble(num2);
    }else {
        System.out.println("Only enter numbers not letters.");
    }

    String calculation;
    switch (operator) {
        case "+":
            calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
            break;
        case "-":
            calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
            break;
        case "*":
            calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
            break;
        case "/":
            calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
            break;
        default:
            calculation = operator + ":" + " Is not a valid operator!";
            break;
    }
    System.out.println(calculation);
}

主要区别在于我只使用了一个 Scanner 对象,并且我用 switch.

替换了嵌套的 if/else

下面是示例运行

Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5.5
Enter your second number: -7.2
5.5 + -7.2 = -1.7000000000000002

其他推荐的改进是:

  1. 循环直到提供有效的运算符。没有理由等到所有输入都提供后再确定由于运算符无效而无法完成计算。
  2. 对每个数字执行类似于上面#1 的操作。
  3. 使用BigDecimal计算。
  4. 格式化结果以显示您想要显示的任意小数值。