0

I get "execution timed out" error ..... why?

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); //take input and output corresponding message /* 1 => Language selection 2 => Customer support 3 => Check the balance 4 => Check loan balance 0 => Exit */ do{ switch(number){ case 1: System.out.println("Language selection"); break; case 2: System.out.println("Customer support"); break; case 3: System.out.println("Check the balance"); break; case 4: System.out.println("Check loan balance"); break; case 0: System.out.println("Exit"); break; } }while (number != 0); } }

31st Jan 2021, 9:11 PM
No Way!
No Way! - avatar
4 Réponses
+ 3
No Way! Your program will enter in switch case no matter what number you enter as do-while loop is exit controlled unlike *for* and *while* loop. As G B already said, your program will never exit the loop if the user enters a non-zero number.
1st Feb 2021, 1:48 AM
Arsenic
Arsenic - avatar
+ 3
No Way! You only take User input once and then go into the do while loop. Inside the loop, you are not updating the input value, so when the first input is not equal to 0, the loop will run for ever.
31st Jan 2021, 9:34 PM
G B
G B - avatar
+ 3
I got it thank you guys
1st Feb 2021, 3:03 PM
No Way!
No Way! - avatar
0
I dont get it, how can I make it work. For me it looks correct. If the number isnt 0 it goes in the switch. And if its 0 it give EXit.
31st Jan 2021, 10:46 PM
No Way!
No Way! - avatar