Basic Java: While loop for basic quiz?

♀尐吖头ヾ 提交于 2019-12-02 00:24:44

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();
  }   
}

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")));

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.

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!

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...


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