Do while loop practice Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Do while loop practice Java

Hello again... next code I stuck and can’t find out what’s wrong is 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 { if(number==1){ System.out.println("Language selection"); } else if (number==2){ System.out.println("Customer support"); } else if (number==3){ System.out.println("Check the balance"); } else if (number==4){ System.out.println("Check loan balance"); } } while (number != 0); System.out.println("Exit"); } } When I run the tests I get >> Execution timed out ! Thank you for your help

24th Jan 2021, 8:23 PM
zer0x0
zer0x0 - avatar
7 Answers
+ 4
well, firstly i would add a way to get input in the do part of the loop so it won’t just take the same number over and over. that would fix the problem because you could make the number 0 and end the program before it goes over the time limit on sololearn. also move the print exit outside of the loop because otherwise it will print every time the loop runs
24th Jan 2021, 8:59 PM
Ollie Q
Ollie Q - avatar
+ 2
hey zer0x0 that is because the while loop is executing infinitely because the condition is never false and that causes the time out error
24th Jan 2021, 8:31 PM
Ollie Q
Ollie Q - avatar
+ 1
Ollie Q any tips to solve this mate
24th Jan 2021, 8:43 PM
zer0x0
zer0x0 - avatar
+ 1
zer0x0 Instead of using if else inside do while loop you can use switch because it will break the loop when condition will satisfy. int number = 0; do { number = scanner.nextInt(); switch (number) { case 1: //Statement break; case 2: //Statement break; case 3: //Statement break; case 4: //Statement break; case 0: //Statement break; } } while (number > 0);
25th Jan 2021, 3:40 AM
A͢J
A͢J - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; do { //take input and output corresponding message /* 1 => Language selection 2 => Customer support 3 => Check the balance 4 => Check loan balance 0 => Exit */ number = scanner.nextInt(); if(number==1) System.out.println("Language selection"); if(number==2) System.out.println("Customer support"); if(number==3) System.out.println("Check the balance"); if(number==4) System.out.println("Check loan balance"); if(number==0) System.out.println("Exit"); } while(number != 0); } }
29th Jun 2021, 6:01 AM
BEDUDURU HARITHA
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; do { number = scanner.nextInt(); switch (number) { case 1 -> System.out.println("Language selection"); case 2 -> System.out.println("Customer support"); case 3 -> System.out.println("Check the balance"); case 4 -> System.out.println("Check loan balance"); case 0 -> System.out.println("Exit"); } } while(number != 0); } }
9th Sep 2021, 4:30 PM
Rangga Raditya
Rangga Raditya - avatar
0
HI all , is there a problem with the proposed solution for the Do..while loop in the java course ? it seems working but it wont compile on the simulator . how can i complete the course ? import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner read = new Scanner(System.in); int password ; do { System.out.println("write password"); password = read.nextInt(); } while (password != 8819); } }
25th Oct 2022, 11:43 PM
salah eddine benabed
salah eddine benabed - avatar