简体   繁体   English

while循环在Java中不起作用

[英]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' . 我试图让这个问题在用户输入'Y'一遍又一遍地问。 and stop and return the Event.displayFinalResults(); 然后停止并返回Event.displayFinalResults(); when the user inputs a 'N' 当用户输入'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. 如果用户回答“否”,则循环将退出(在询问另外一个金额值之后),但不会执行Event.displayFinalResults() Drop the else clause, so Event.displayFinalResults() whether it entered the if statement or not. 删除else子句,以便Event.displayFinalResults()是否输入if语句。

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; 您可以使用break;执行此操作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. 我看到的一个问题是,您的while循环位于if语句内部。 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. 一旦退出while循环,就不会在else块中运行代码,因为if条件已经为真。

So try removing the else block. 因此,请尝试删除else块。 This will make sure the Event.displayFinalResults method is called once the while loop exits. 一旦while循环退出,这将确保Event.displayFinalResults方法被调用。

     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... 如果要询问,请调用此方法,而不是循环执行...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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