Java中同时执行if和else语句。

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

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(&quot;Enter a first number&quot;);
int FirstNum = in.nextInt();
System.out.println(&quot;Enter an Operator&quot;);
String Operator = in.next();
System.out.println(&quot;Enter a second number&quot;);
int SecondNum = in.nextInt();
try{
if(Operator.equals(&quot;+&quot;)){
System.out.println(FirstNum + SecondNum);
}
if (Operator.equals(&quot;-&quot;)){
System.out.println(FirstNum - SecondNum);
}
if (Operator.equals(&quot;*&quot;)){
System.out.println(FirstNum * SecondNum);
}
if (Operator.equals(&quot;/&quot;)) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println(&quot;invalid Operator&quot;);
}
}
catch (Exception e){
System.out.println(&quot;Invalid Operator or number&quot;);
}
}

}

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(&quot;+&quot;)){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals(&quot;-&quot;)){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals(&quot;*&quot;)){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals(&quot;/&quot;)) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println(&quot;invalid Operator&quot;);
}

答案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 elsestatement.

You need an if/else if/else statement

if(Operator.equals(&quot;+&quot;)){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals(&quot;-&quot;)){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals(&quot;*&quot;)){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals(&quot;/&quot;)) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println(&quot;invalid Operator&quot;);
}

答案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(&quot;+&quot;)){
System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals(&quot;-&quot;)){
System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals(&quot;*&quot;)){
System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals(&quot;/&quot;)) {
System.out.println(FirstNum / SecondNum);
}
else {
System.out.println(&quot;invalid Operator&quot;);
}
}
catch (Exception e){
System.out.println(&quot;Invalid Operator or number&quot;);
}

huangapple
  • 本文由 发表于 2020年10月16日 06:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64380599.html
匿名

发表评论

匿名网友

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

确定