简体   繁体   English

Java if Statement,'Else'仍在执行

[英]Java if Statement, 'Else' Executing Anyway

I am studying Java, hoping to some day be good at it. 我正在学习Java,希望有一天能够擅长Java。 Right now I'm playing with If Statements. 现在我正在玩If Statements。 I made the following code while playing around: 我在玩游戏时制作了以下代码:

import java.util.Scanner;

class programBegin {
public static void main(String args[]) {

    int myNum;

    Scanner sc = new Scanner(System.in); // Creates variable sc as type Scanner
    System.out.println("Enter a number between 1 and 10: ");
    myNum = sc.nextInt(); // Requests input and will store it as type int.

    if (myNum > 5) {
        System.out.println("Your number is greater than 5.");
    }
    if (myNum == 5) {
        System.out.println("your number is equal to 5.");
    } else {
        System.out.println("Your number is less than 5.");
    }
  }
}

If I enter 6 for example, the output I get is: 例如,如果我输入6,我得到的输出是:

Your number is greater than 5. 您的电话号码大于5。

Your number is less than 5. 您的电话号码小于5。

I thought else wasn't supposed to execute unless the previous statement was false? 除非之前的陈述是假的,否则我认为不应该执行其他事情?

You're missing an else before the if (myNum == 5) . 你在if (myNum == 5)之前错过了一个else

if (myNum > 5) {
    System.out.println("Your number is greater than 5.");
} else if (myNum == 5) {
    System.out.println("your number is equal to 5.");
} else {
    System.out.println("Your number is less than 5.");
}

change it to else if 如果,将其更改为else

if (myNum > 5) {

} else if (myNum == 5) {

} else {

}

如果你在第二个if之前添加一个else,它将按预期工作。

Your code is the equivalent of "if number is over 5, print this. if number is 5, print this. if number is not 5, print this." 您的代码相当于“如果数字超过5,则打印此数据。如果数字为5,则打印此数据。如果数字不是5,则打印此数据。”

Your else statement is corresponding to if (myNum == 5) rather than if (myNum > 5) . 你的else语句对应于if (myNum == 5)而不是if (myNum > 5)

You probably wish to use an if-else statement here. 你可能希望在这里使用if-else语句。

if (myNum > 5) {
    System.out.println("Your number is greater than 5.");
} else if (myNum == 5) {
    System.out.println("your number is equal to 5.");
} else {
    System.out.println("Your number is less than 5.");
}

After the first if ..... its not existing the block .... It also checks the 2nd if...and as its a false..it enter the else... 在第一个if ..... 它不存在块 ....它还检查第二个if ...并且作为它的假..它进入其他...

Try it this way.... 试试这种方式....

if (myNum > 5) {
        System.out.println("Your number is greater than 5.");
    }
    if (myNum == 5) {
        System.out.println("your number is equal to 5.");
    } 
   else if(myNum < 5) {
        System.out.println("Your number is less than 5.");
    }

Or 要么

   if (myNum > 5) {
            System.out.println("Your number is greater than 5.");
        }
        else if (myNum == 5) {
            System.out.println("your number is equal to 5.");
        } 
       else (myNum < 5) {
            System.out.println("Your number is less than 5.");
        }

你需要把你的第二个if if变成else if语句,因为第二个if总是被检查并且它返回false因为6!= 5,这意味着你的其他人将被评估。

you can use it for more performance. 您可以使用它来获得更高的性能。

here if mynum is greater than 5 , two other ifs would be omitted: 这里如果mynum大于5,则会省略另外两个ifs:

if (myNum > 5) {
    System.out.println("Your number is greater than 5.");
} else{
    if (myNum == 5) {
        System.out.println("your number is equal to 5.");
    } else if(myNum < 5) {
        System.out.println("Your number is less than 5.");
    }
}

Those are two different 'if' statements being executed instead of one. 这是两个不同的'if'语句被执行而不是一个。 There are many ways to fix this...see the many different ways above :) Its interesting to know that not putting one word can have a different output or in this case, more than one output. 有很多方法可以解决这个问题...请参阅上面的许多不同方法:)有趣的是,知道不放一个单词可以有不同的输出,或者在这种情况下,输出多个。 In this case, 'else if' will be a good way of fixing the minor bug. 在这种情况下,'else if'将是修复小错误的好方法。 Good luck with your learning process. 祝你学习顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM