简体   繁体   中英

while loop not working in Java

I am trying to get this to ask the question over and over again while the user inputs a 'Y' . and stop and return the Event.displayFinalResults(); when the user inputs a 'N'

i am getting a continuous loop right now. I am missing something or my layout it wrong. Any help would be great.

            System.out.println("Is there anymore amounts you want to add?(Y/N): ");
            Scanner keyboard = new Scanner(System.in);
            char choice = keyboard.next().charAt(0);
            choice = Character.toUpperCase(choice);
            if(choice == 'Y')
            {
                do
                {
                    System.out.println("Is there anymore amounts you want to add?(Y/N): ");
                    choice = keyboard.next().charAt(0);
                    choice = Character.toUpperCase(choice);

                    readValidateAmountType();
                    readValidateAmount(); 
                }
                while(choice =='Y');

            }
            else
            {
                Event.displayFinalResults();
            }

thanks again.

You program asks a Yes/No question, then if you answer Yes, it enter a loop which starts by asking the same question again, before asking for the amount value.

You might want to ask for the amount value before asking the Yes/No question again.

If user answer No, the loop will exit (after asking for one more amount value), but Event.displayFinalResults() will not be executed. Drop the else clause, so Event.displayFinalResults() whether it entered the if statement or not.

System.out.println("Is there anymore amounts you want to add?(Y/N): ");
Scanner keyboard = new Scanner(System.in);
char choice = keyboard.next().charAt(0);
choice = Character.toUpperCase(choice);
if (choice == 'Y')
{
    do
    {
        readValidateAmountType();
        readValidateAmount(); 

        System.out.println("Is there anymore amounts you want to add?(Y/N): ");
        choice = keyboard.next().charAt(0);
        choice = Character.toUpperCase(choice);
    }
    while(choice =='Y');
}
Event.displayFinalResults();

You could do this using break; as follows:

do {
    System.out.println("Is there anymore amounts you want to add?(Y/N): ");
    choice = keyboard.next().charAt(0);
    choice = Character.toUpperCase(choice);
    if (choice !='Y') {
        break;
    }

    readValidateAmountType();
    readValidateAmount(); 
} while(true);

Event.displayFinalResults();

One problem I see with this is, your while loop is inside the if statement. Once you exit the while loop, it's NOT going to run the code inside the else block because the if condition was already true.

So try removing the else block. This will make sure the Event.displayFinalResults method is called once the while loop exits.

     System.out.println("Is there anymore amounts you want to add?(Y/N): ");
        Scanner keyboard = new Scanner(System.in);
        char choice = keyboard.next().charAt(0);
        choice = Character.toUpperCase(choice);

if(choice == 'Y')
        {
            do
            {
                System.out.println("Is there anymore amounts you want to add?(Y/N): ");
                choice = keyboard.next().charAt(0);
                choice = Character.toUpperCase(choice);

                readValidateAmountType();
                readValidateAmount(); 
            }
            while(choice =='Y');

        }

        Event.displayFinalResults();

Clear code and compact:

Scanner keyboard = new Scanner(System.in);
char choice;
do {
   System.out.println("Is there anymore amounts you want to add?(Y/N): ");
   choice = keyboard.next().charAt(0);
   choice = Character.toUpperCase(choice);
   if(choice == 'Y') {
      readValidateAmountType();
      readValidateAmount(); 
   }
} while(choice == 'Y');     
Event.displayFinalResults();   

Try the following out:

    public void answering(char answer){
        if(answer == 'Y' || answer == 'y'){
            System.out.println("Answering");
        } else if(answer == 'N' || answer == 'n'){
            System.out.println("Not answering");
    }

Instead of looping, call this method when you want to ask...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM