Using switch statements in a while loop

2020-05-06 13:10发布

问题:

I'm trying to use switch statements in a while loop in Java, but there is something going wrong. Please have a look at a sample code below which explains my problem:

Scanner input=new Scanner(System.in);
    int selection = input.nextInt();

while (selection<4)
      {  switch(selection){
            case 1:
               System.out.println("Please enter amount");
               double amount=input.nextDouble(); //object of scanner class
               break;

            case 2:
               System.out.println("Enter ID number"); 
               break;

            case 3:
               System.out.println("Enter amount to be credited");
               break;
                          }
System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
     }

If I run this code, the output is as follows:

1
Please enter amount
2000
1. Transfer
2.Check balance
3.Recharge
Please enter amount
2
1. Transfer
2.Check balance
3.Recharge
Please enter amount

When I enter the amount, I would then like to choose another option - and the output should be according to the option chosen (you should probably be knowing what I want this code to do). Could someone please help correct the code?

Thanks

回答1:

You're forgetting to ask for the selection again. It's not going to change once it's been entered.

Scanner input=new Scanner(System.in);
int selection = input.nextInt();

while (selection<4)
{
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
      selection = input.nextInt(); // add this
 }

You could even use a do...while loop instead to avoid writing input.nextInt(); twice

Scanner input=new Scanner(System.in);
int selection;

do
{
   selection = input.nextInt();
   switch(selection){
        case 1:
           System.out.println("Please enter amount");
           double amount=input.nextDouble(); //object of scanner class
           break;

        case 2:
           System.out.println("Enter ID number"); 
           break;

        case 3:
           System.out.println("Enter amount to be credited");
           break;
      }
      System.out.println("1. Transfer\n2.Check balance\n3.Recharge");
 }
 while(selection < 4);


回答2:

You currently get and set the selection value once and before the while loop, and so there is no way to change this from within the loop. The solution: Get your next selection value from the Scanner object inside of the while loop. To understand this, think the problem out logically and be sure to walk through your code mentally and on paper as the issue is not really a programming issue but rather a basic logic issue.


Regarding:

Could someone please help correct the code?

Please don't ask us to do this and for several reasons.

  1. This is not a homework completion service
  2. You're harming yourself by asking others to change the code for you, as you learn how to code by writing code.
  3. Really this is a basic simple issue that you have the ability to fix on your own. Please give it a try, and only if the attempt doesn't work, then show us your attempt.


回答3:

Case must be bigger than 4, in your case the cases are less than 4. so you won't quit the loop, basically the break statement breaks the switch and jumps to loop, but than the loop is again less than 4 so it jumps again into the switch and so on. Fix the Sizes of your cases, maybe just make an

(selection != 1 || selection != 2 || selection !=3 || selection !=4)