即使我在 if 语句之外定义了变量(简单计算器),我的程序也找不到 if 语句内的变量
我添加了一个 if 语句,它将输入的数字解析为双精度数,以便在第二个 if 语句块中进行计算时使用,如果仅输入数字而不是字母,则程序必须运行,程序似乎无法读取当我输入 double(5.5) 时,效果很好,但当我输入 int(5) 数字时,效果很好。
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
I added a if statement that parses a entered number into a double to use when I do my calculations in the second if statement block, the program has to run if only numbers where entered and not letters, the program doesn't seem to read the numbers when I entered a double(5.5) but works fine when I enter a 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]+quot;);
boolean check2 = num2.trim().matches("^[0-9]+quot;);
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);
}
I think its maybe the ( . ) thats the problem,
my output to console
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
And now the int numbers that works fine.
Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5
Enter your second number: 5
5.0 + 5.0 = 10.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用多个
Scanner
对象。不需要多个,而且这也可能会产生一些您不想处理的副作用。由于您正在使用正则表达式,并且其他人建议了其他解决方案,因此我决定向您展示如何使用正则表达式来计算浮点值。正确的表达式是
或更好...
此表达式计算一个值,该值可能包含可选符号(正或负),后跟零个或多个数字,然后是可选小数点和无限数量的小数位。但是,如果使用小数点,则必须至少出现一位数字。在像
+2.
这样的情况下,仅匹配+2
。下面的代码是您的解决方案版本,稍加修改:
主要区别是我只使用了一个
Scanner
对象,并且我用if/else
替换了嵌套的if/else
。代码>开关。下面是一个示例运行
其他建议的改进是:
BigDecimal
进行计算。Do not use more than one
Scanner
object. There is no need for more than one, and this could also have some side effects you don't want to deal with.Since you are using regular expression and others have suggested other solutions, I decided to show you how to use regular expression to evaluate a floating-point value. The correct expression for this is
or better yet...
This expression evaluates a value that may contain an optional sign (positive or negative) followed by zero or more digits, then followed by optional decimal point and an unlimited number of decimal digits. However, if the decimal point is used, at least one digit must be present. In a case like
+2.
, only the+2
is matched.The code below is your version of the solution, slightly modified:
The main differences are that I used only one
Scanner
object and that I replaced your nestedif/else
with aswitch
.Below is a sample run
Other recommended improvements are:
BigDecimal
the calculation.正则表达式“^[0-9]+$”仅对于整数计算为 true,因为它检查字符串是否由 0 - 9 组成。如果您还想检查双精度数,这可能会有所帮助:https://www.baeldung.com/java-check-string-number
第一个建议这可能是最简单的。用 try catch 包围 Double.parseDouble ,因为如果它不是数字,它会给你一个 NumberFormatException 。
The regular expression "^[0-9]+$" only evaluates to true for integers, because it checks whether the string consists of 0 - 9. If you want to check for doubles as well, this might help: https://www.baeldung.com/java-check-string-number
The first suggestion there is probably the easiest. Surround Double.parseDouble with a try catch, because it will give you an NumberFormatException if its not a number.