简体   繁体   中英

Java if Statement, 'Else' Executing Anyway

I am studying Java, hoping to some day be good at it. Right now I'm playing with 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:

Your number is greater than 5.

Your number is less than 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) {
    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

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."

Your else statement is corresponding to if (myNum == 5) rather than if (myNum > 5) .

You probably wish to use an if-else statement here.

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...

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:

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. 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. Good luck with your learning process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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