Can anybody tell why the if statement is not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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"); } } } } } }

17th Aug 2019, 7:02 AM
Ankush Raj
Ankush Raj - avatar
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");
17th Aug 2019, 7:19 AM
voja
voja - avatar