To give you an example of a better code:
int number1 = scanner.nextInt();
int number2 = 0;
while (true) {
number2 = scanner.nextInt();
if (number1 == number2){
System.out.println("Woo!");
break;
}
number1 = number2;
}
Explanation:
This code will always compare the two last entered numbers. (e.g. 1,2,2
will print Woo!
).
For better legibility I changed your for loop into a while loop.number1 == number2
returns always the same as number2 == number1
. That’s why number1 == number2 || number2 == number1
isn’t needed.
0
solved Comparing 2 numbers in a loop not working correctly