Answer the question
In order to leave comments, you need to log in
How to terminate a Java program when the user enters the character q anywhere in the program?
The task is to write a simple calculator (number-operation-number-result). The program should run indefinitely if the user enters the correct data, but if the user enters the letter q then the program should terminate (exactly terminate, not notify of an error). Here's what I have so far:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do {
System.out.println("Введите число");
int a = sc.nextInt();
sc.nextLine();
System.out.println("Введите операцию");
String s = sc.nextLine();
System.out.println("Введите число");
int b = sc.nextInt();
if (s.equals("+")) {
int res = 0;
res = a + b;
System.out.println(res);
}
char c = s.charAt(0);
if (c == ('q'))
break;
} while(true);
Answer the question
In order to leave comments, you need to log in
if (c == ('q'))
break;
System.exit(0);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question