0
Can anybody tell why the if statement is not working?
import java.util.Scanner; public class Test { public static void main (String args []) { Scanner input = new Scanner(System.in); { System.out.println("enter two numbers"); { int x; x = input.nextInt(); int y; y = input.nextInt(); if (x==y) { System.out.println("x is equal to y"); if (x!=y) { System.out.println("x is not equal to y"); } } } } } }
1 Answer
+ 4
Check your curly brackets:
if (x==y) {
System.out.println("x is equal to y");
}
if (x!=y) {
System.out.println("x is not equal to y");
}
Also, you could write it like this, there's no need for two if statements:
if (x==y)
System.out.println("x is equal to y");
else
System.out.println("x is not equal to y");