+ 1

User can enter any number any times but user input 0 program will be terminated

java code

29th Oct 2017, 5:53 AM
Ayesha
Ayesha - avatar
2 Respuestas
+ 2
You could also do: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); while(num != 0) { num = sc.nextInt(); } } } Or: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { int num = sc.nextInt(); if(num == 0) { break; } } } } @luka FYI there is usually no need to import java.lang as it is implicitly imported in Java programs anyhow. This is why you can use System.out.println() without an import.
29th Oct 2017, 6:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
@luka true, lol, but it will exit by default if it is the last part of the code before the end of main. Just a technicality i guess.
29th Oct 2017, 6:41 AM
ChaoticDawg
ChaoticDawg - avatar