0

Why isn't this running and has an error?

import java.util.Scanner; public class Program{ public static void main(String[] args) { int x; System.out.println("Enter a number"); Scanner in = new scanner(System.in); x = in.nextInt(); while(x>=0){ System.out.println(x); if(x%2==0){ System.out.println("The number is even"); int x; System.out.println("Enter a number"); Scanner in = new scanner(System.in); x = in.nextInt(); while(x>=0){ System.out.println(x); if(x%2==1){ System.out.println("The number is odd"); x--; } } }

6th Dec 2019, 6:59 AM
strykds
2 Antworten
+ 1
You are defining x variable and 'in' object two times, already defined not needed again to define. Remove 2nd defined Scanner object in, statement. And int x; You can directly overwrite x , and use in.... and typo Capital S in Scanner. and end braces are missing... and x-- ; calculating in if only executed ones when x is odd, then it is infinite loop because it is in while. So change all these. Run it the playground and share the link here.. ok.
6th Dec 2019, 8:49 AM
Jayakrishna 🇮🇳
+ 1
import java.util.Scanner; public class Program{ public static void main(String[] args) { int x; System.out.println("Enter a number"); Scanner in = new Scanner(System.in); x = in.nextInt(); while(x>=0){ System.out.println(x); if(x%2==0){ System.out.println("The number is even"); System.out.println("Enter a number"); x = in.nextInt(); while(x>=0){ System.out.println(x); if(x%2==1){ System.out.println("The number is odd");} x--; } }}} } // is this what you wanted code?
6th Dec 2019, 8:55 AM
Jayakrishna 🇮🇳