First if statement always true, but second one is not

Yoshi24517 :

I humbly do apologize if this has been asked before.

Here is some code that I have.

    int id1 = 1234;
    int id2 = 5678;
    int idPass1 = 123;
    int idPass2 = 456;

    System.out.println("Welcome. Please enter your Employee ID.");
    while(true) {
        int id = input.nextInt();
        //add more if needed
        if(id != id1 || id != id2){
            System.out.println("Employee does not exist. Please try again.");
        } else {
            break;
        }
    }
    System.out.println("Please enter your password.");
    while(true){
        int idPass = input.nextInt();
        //add more if needed
        if(idPass != idPass1 || idPass != idPass2){
            System.out.println("Incorrect Password entered. Please try again.");
        } else {
            break;
        }
    }

As you can see. the 2 if/else statements are exactly the same, except for the variables. The first statement with the IDs, in IntelliJ, says that it is always true. The second statement, with the passwords, does not give me that warning.

How can the first if/else statement always be true if the second if/else statement is exactly the same (minus the variables) and is not always true?

(Scanner was declared prior to this code.)

lincr :

Since your id1 and id2 are not equal, the expression (id != id1 || id != id2)should always be evaluated to true. You probably want to use && to replace ||. And then your code falls in the first loop, leading to the second expression not evaluated, so no warning.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85818&siteId=1