Help. Java Do While Loop | Sololearn: Learn to code for FREE!

0

Help. Java Do While Loop

My code prints output continuously and doesn't ask for another input. Please help. You are creating an automated phone system for bank clients. Number selections should activate the actions noted below as follows: 1 => Language selection 2 => Customer support 3 => Check account balance 4 => Check loan balance 0 => Exit You can use the first 4 commands in a random sequence without interrupting the phone call - only the number 0 does. Write a program that will continuously take a number as input and output the corresponding message, until the client enters 0. Sample Input 1 4 3 0 Sample Output Language selection Check loan balance Check the balance Exit https://code.sololearn.com/c8P8gZSrTPor/?ref=app https://code.sololearn.com/c8P8gZSrTPor/?ref=app https://code.sololearn.com/c8P8gZSrTPor/?ref=app

2/28/2021 5:08:30 PM

Rjmcdxx

5 Answers

New Answer

+3

This way also work 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"); 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"); } } //take input and output corresponding message /* 1 => Language selection 2 => Customer support 3 => Check the balance 4 => Check loan balance 0 => Exit */ } while(number != 0); } }

+1

Rjmcdxx Take input inside loop and check while (number > 0) And also you have to write case 0

0

number input as to given inside the do loop. given outside the loop it runs only the first input so it doesn't break the loop(only in case u give 0 as the first input) and continuously runs the given first input https://code.sololearn.com/cA02A10A13a1

0

Would be nice if the task and the example were consistent. This is the second time I catch an error like that today. 4 => Check *loan* balance VS. Check *the* balance