This code is not making loop. Upon getting wrong answer, it is not going back to question. Help is appreciated.
package packageMCQ;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
String correctAnswer = "beaver";
Scanner scanner = new Scanner(System.in);
String answer = "";
while (true) {
System.out.println("What is the national animal of Canada?");
answer = scanner.next();
if (answer.equals(correctAnswer.toLowerCase()))
System.out.println("You are right");
else
System.out.println("Try again");
break;
}
}
}
Thankyou. It’s working now. The only thing is
answer.equals(correctAnswer.toLowerCase())
was not working. So I had to add a lion. Now the code is:
public static void main(String[] args) {
// TODO Auto-generated method stub
String correctAnswer = "beaver";
Scanner scanner = new Scanner(System.in);
String answer = "";
while (true) {
System.out.print("What is the national animal of Canada? ");
answer = scanner.next();
String answer1 = answer.toLowerCase(); *****This line was added
if (answer1.equals(correctAnswer)) {
System.out.println("You are right");
break;
}
System.out.println("Try again");
}
}