英文:
Java both If and else statements are executed
问题
我用Java写了一个小型控制台应用程序,它由if和else语句组成。<br>
在输出中,正确的if和else语句都被执行。<br>
如何修复?问题是什么?
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入第一个数字");
int FirstNum = in.nextInt();
System.out.println("输入运算符");
String Operator = in.next();
System.out.println("输入第二个数字");
int SecondNum = in.nextInt();
try {
if (Operator.equals("+")) {
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals("-")) {
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals("*")) {
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println("无效的运算符");
}
} catch (Exception e) {
System.out.println("无效的运算符或数字");
}
}
输出示例:
输入第一个数字
1
输入运算符
+
输入第二个数字
2
3
无效的运算符
进程以退出代码0结束
JDK:java 15
英文:
I wrote a little console app in java, which is made of If and else statements.<br>
And on the output, both of the right If and else statements are executed.<br>
How is it possible to fix? what's the problem?
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a first number");
int FirstNum = in.nextInt();
System.out.println("Enter an Operator");
String Operator = in.next();
System.out.println("Enter a second number");
int SecondNum = in.nextInt();
try{
if(Operator.equals("+")){
System.out.println(FirstNum + SecondNum);
}
if (Operator.equals("-")){
System.out.println(FirstNum - SecondNum);
}
if (Operator.equals("*")){
System.out.println(FirstNum * SecondNum);
}
if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println("invalid Operator");
}
}
catch (Exception e){
System.out.println("Invalid Operator or number");
}
}
}
Output example:
Enter a first number
1
Enter an Operator
+
Enter a second number
2
3
invalid Operator
Process finished with exit code 0
JDK: java 15
答案1
得分: 4
当编写类似这样的条件语句时,您需要使用else if
将它们连接起来,基本上是每个if
对应一个else
,最后跟一个else
。发生的情况是,您在表达:“如果这样。并且如果这样。并且如果这样”等等。它会测试最后一个if
,由于它不是正斜杠,所以您会进入else
块。
尝试这样写:
if (Operator.equals("+")) {
System.out.println(FirstNum + SecondNum);
} else if (Operator.equals("-")) {
System.out.println(FirstNum - SecondNum);
} else if (Operator.equals("*")) {
System.out.println(FirstNum * SecondNum);
} else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
} else {
System.out.println("无效的操作符");
}
英文:
When writing if statements like this, you need to chain them together with else if
, basically an else
for every if
with a final else
. What is happening is you are saying "if this. And if this. And if this" etc. It is testing the last if
and since it is not a forward slash, you are getting into the else.
Try this:
if(Operator.equals("+")){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals("-")){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals("*")){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println("invalid Operator");
}
答案2
得分: 2
首先:无法同时执行这两个语句。
那么,发生了什么?if/else 结构仅出现在最后两行。也就是说:如果运算符不是 /
,代码将执行 else
语句。
您需要一个 if/else if/else 语句:
if (Operator.equals("+")) {
System.out.println(FirstNum + SecondNum);
} else if (Operator.equals("-")) {
System.out.println(FirstNum - SecondNum);
} else if (Operator.equals("*")) {
System.out.println(FirstNum * SecondNum);
} else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
} else {
System.out.println("无效的运算符");
}
英文:
First of all: Is not possible execute both statements.
So, ¿what's happen? The structure if/else is only the last two lines. That is: If the operator is not /
the code will execute the else
statement.
You need an if/else if/else statement
if(Operator.equals("+")){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals("-")){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals("*")){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println("invalid Operator");
}
答案3
得分: 2
你应该在后续的语句中使用 else if
。如果你只是使用 if ... if ... if ... else
,那么实际上每个 if
语句都会被单独评估,只有最后的 if else
会作为一个整体被执行其中的一个。
try {
if (Operator.equals("+")) {
System.out.println(FirstNum + SecondNum);
} else if (Operator.equals("-")) {
System.out.println(FirstNum - SecondNum);
} else if (Operator.equals("*")) {
System.out.println(FirstNum * SecondNum);
} else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
} else {
System.out.println("无效的运算符");
}
} catch (Exception e) {
System.out.println("无效的运算符或数字");
}
英文:
You should be using else if
for the subsequent statements. If you just use if ... if ... if ... else
, then essentially each if
is evaluated separately and only the FINAL if else
is put together as one or the other.
try{
if(Operator.equals("+")){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals("-")){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals("*")){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals("/")) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println("invalid Operator");
}
}
catch (Exception e){
System.out.println("Invalid Operator or number");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论