Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Benneth Paredis
4,597 PointsI'm making a simple quiz game but i have an error can somebody help me please.
It keeps saying the answer is incorrect while i clearly typed yellow.
import java.io.Console; public class quizGame { public static void main(String[] args) { Console console = System.console(); console.printf("Welcome to my quiz game\n"); console.printf("-------------------------\n"); String answer = console.readLine("What is the color of the sun?\n"); if (answer == "yellow") { console.printf("Well done!"); }else { console.printf("Sorry but the answer is incorrect!"); }
}
}
Benneth Paredis
4,597 Pointsthanks you it is working now :)
Derek Markman
16,291 PointsSince whatever you argument you put inside of the equals method is case sensitive, you also might want to consider adding before your conditional statement:
answer = answer.toLowerCase();
Now, it wont matter if the user types in "YELLOW", "YeLlOw" etc.... it will always be the correct answer. Assuming of course, the equals method argument is "yellow".
Benneth Paredis
4,597 PointsThanks , one more thing. How do i make it so when the answer is wrong , it goes back to the question.
Derek Markman
16,291 PointsYou can do that a few different ways, one way would be to put it inside of a while loop like so:
import java.io.Console;
public class Quiz {
public static String answer;
static Console console = System.console();
public static boolean quizEnd = false;
public static void main(String[] args) {
while(quizEnd == false) {
answer = console.readLine("What color is the sun?: ");
answer = answer.toLowerCase();
if(answer.equals("yellow")) {
quizEnd = true;
} else {
quizEnd = false;
}
}
}
}
This way of doing it basically will prompt for the answer over and over again until the user types in the correct choice.
To expand your quiz game for other questions you can keep each question in its own method like so:
import java.io.Console;
public class Quiz {
public static String question1;
public static String question2;
static Console console = System.console();
public static boolean quizEnd = false;
public static void main(String[] args) {
while(quizEnd == false) {
question1();
}
}
public static void question1() {
question1 = console.readLine("what is the color of the sun?: ");
question1 = question1.toLowerCase();
if(question1.equals("yellow")) {
System.out.println("nice job");
//quizEnd = true; - will end the quiz game entirely
//question2(); - will start the next question
} else {
System.out.println("sorry thats wrong try again");
question1(); // restarts the question1 method
//quizEnd = true; - ends the quiz game
}
}
public static void question2() {
question2 = console.readLine("what color is the sky?: ");
question2 = question2.toLowerCase();
if(question2.equals("blue")) {
System.out.println("nice job");
//quizEnd = true; - this will end the quiz game
//or you can add another question method like the other 2
//question3(); etc...
} else {
System.out.println("sorry thats wrong try again");
question2(); //will start question2 method again
//question1(); - will start the question1 method again
//quizEnd = true; - ends the quiz game
}
}
}
The second way can be made a lot cleaner, seeing as how it uses a lot of repeated code. This was just to give you an idea of how to do this.
Keep in mind both of these methods I've shown use an infinite while loop, and if at one point in your conditional statements you don't set quizEnd to true, it will just keep prompting over and over again.
Benneth Paredis
4,597 PointsMany thanks this helped me alot! you should be teacher at treehouse :D
Kai Tait
3,717 PointsKai Tait
3,717 PointsTry replacing
if (answer == "yellow")with
if (answer.equals("yellow"))== checks if both objects point to the same memory location .equals() compares the objects