Hello forum, new here and a beginner to java. I have a small program with input, output and a switch statement.
What I need, is for the program to take in answers and compare them to the variable.
Code:
import java.util.Scanner;
public class Dutch {
public static void main(String[] args)
{
answer();
}
static void answer()
{
Scanner input = new Scanner(System.in);
String name = "";
String theAnswer = "";
boolean answering = true;
System.out.println("Enter answers: " + "\n");
while(answering == true)
{
switch(1)
{
case 1:
{
while(answering == true)
{
theAnswer = input.nextLine();
if(theAnswer.equals("hond") || theAnswer.equals("Hond"))
{
System.out.println("Well done! You are correct. Next one." + "\n");
} else
{
System.out.println("Oops! Try again!" + "\n");
}
}
}
case 2:
{
while(answering == true)
{
theAnswer = input.nextLine();
if(theAnswer.equals("kat") || theAnswer.equals("Kat"))
{
System.out.println("Well done! You are correct. Next one." + "\n");
} else
{
System.out.println("Oops! Try again!" + "\n");
}
}
}
case 3:
while(answering = true)
{
theAnswer = input.nextLine();
if(theAnswer.equals("vis") || theAnswer.equals("Vis"))
{
System.out.println("Well done! You are correct. Next one." + "\n");
} else
System.out.println("Oops! Try again!" + "\n");
}
}
}
}
}
But when I get to "kat" all that comes up is "Oops! Try again!" completely bypassing the original if statement, the first case works as needed, but the rest just don't.. I've been trying to work it out for a few hours now but the code seems fine, maybe just my logic?
Any help is appreciated, thanks!