+ 1
User can enter any number any times but user input 0 program will be terminated
java code
5 Answers
+ 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.
+ 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.