Basic Java: While loop for basic quiz?

冷暖自知 提交于 2019-12-02 03:22:41

问题


I started learning Java last night, and I'm trying to make my first code without it being dictated to me!

It is a simple quiz question that asks the user "Do you think my dog is cute?" If they answer "Yes," the dog Woofs and smiles. If they say "no" He growls at them and frowns. If a different answer is given, "What? Please try again" is printed.

I have no issue getting this working so far, but now I am trying to make it so that when an unrecognised answer is put in, the question is asked again, and this repeats until they answer either "Yes" or "No." Here is what I have tried:

import java.util.Scanner;
public class CopyOfMain {
    public static void main(String joel[]){
        System.out.println("Do you think my dog Olly is cute? Yes or No");
        Scanner ans = new Scanner(System.in);
        String Ans = ans.nextLine();
        while (!Ans.equals("Yes") || !Ans.equals("No")); {
            if (Ans.equals("Yes")){
                System.out.print("Woof :-)");

            }else if(Ans.equals("No")){
                System.out.print("Grrrr :-(");

            }else{  
                System.out.print("What? Please try again.");
            }   
        }
    ans.close();
    }
}

All I have added to the working code is the while loop, but now if "Yes" or "No" is entered, there is no printed woof or growl. However, if something other than "Yes" or "No" is entered, the correct words and printed, but the question is still not re-asked. Could someone please suggest a fix?


回答1:


Re-read the line in the loop (also use an AND not an OR nothing is both "Yes" and "No" or an the simpler infinite loop and break), also - I would use another variable Ans and ans are too similar for my tastes, Additionally, note that I'm testing your strings independent of case (I assume "yes" and "YES" should also work). Finally, no semicolon after while and before the open bracket (that makes an empty loop) -

String response = ans.nextLine();
while (true) {
  if (response.equalsIgnoreCase("Yes")){
    System.out.println("Woof :-)");
    break;
  }else if(response.equalsIgnoreCase("No")){
    System.out.println("Grrrr :-(");
    break;
  }else{  
    System.out.println("What? Please try again.");
    response = ans.nextLine();
  }   
}



回答2:


You have to be able to check the result first, then check for the loop condition:

String Ans;

do {
    Ans = ans.nextLine()

    if (Ans.equals("Yes")) {
        System.out.print("Woof :-)");
    } else if(Ans.equals("No")) {
        System.out.print("Grrrr :-(");
    } else {  
        System.out.print("What? Please try again.");
    }
} while (!(Ans.equals("Yes") || Ans.equals("No")));



回答3:


Your while loop will always evaluate to true. If you enter "Yes", then it's not "No", and vice-versa.

When exactly do you want the while loop to be true? Say it out loud and write it down on a piece of paper before trying to work out the boolean logic.

You also need to read the answer again inside the while loop, otherwise it will never change.




回答4:


Solved! via combination of the two good answers. I used the code from this answer, and incorporated do..while from the other answer. Then I made a new If and else if statement under the existing "else" statement! Thankyou everyone for your kind and speedy help.

P.s any easier way than two seperate if/else statements?

I will put my final code here for anyone who has a similar question to me.

import java.util.Scanner;
public class CopyOfMain {
    public static void main(String joel[]){
        System.out.println("Do you think my dog Olly is cute? Yes or No");
        Scanner ans = new Scanner(System.in);
        String response = ans.nextLine();

        {
        do  {
            if (response.equalsIgnoreCase("Yes")){
                System.out.print("Woof :-)");

            }else if(response.equalsIgnoreCase("No")){
                System.out.print("Grrrr :-(");

            }else{  
                System.out.print("What? Please try again.");
                response = ans.nextLine();
                if (response.equalsIgnoreCase("Yes")){
                    System.out.print("Woof :-)");

                }else if(response.equalsIgnoreCase("No")){
                    System.out.print("Grrrr :-(");
                }
            }
          }  while (!response.equalsIgnoreCase("Yes") && !response.equalsIgnoreCase("No")); 
        }
        ans.close();
    }
}

EDIT: Elliot Frisch has solved the problem well, with a shorter code. Look at his edited answer!




回答5:


The most concise way of going about this would probably be a multidimensional array. Think of it as an array inside of an array.

String arr[] = [["question","answer"], ["question","answer"]]; ...etc

Using this method you only need one small loop:

for (x=0;questions < arr.length; x++) {
System.out.print(arr[x][x]);

if (arr[x][x+1] == true)
score++;

else...


}


来源:https://stackoverflow.com/questions/24436562/basic-java-while-loop-for-basic-quiz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!