So for the last part of my code, I'm trying to make a comment based on the number of questions the user got correct. If the user got 3 or more correct, I want to leave a congrats. But if the user got less than that, I want to leave a comment to study a bit more. I know I'm supposed to use relational operators to solve this but I'm kind of perplexed as to how I actually do it.
import java.util.Scanner;
public class quizcreation
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
System.out.println ("If your answer is not one of the options, it will be considered incorrect.");
System.out.println();
//Question 1
System.out.println ("T/F: Harry Potter was an only child.");
String answer1 = input.next();
if (answer1.equals("True"))
{
System.out.println("That is correct! Harry was an only child.");
}
else if (answer1.equals("true"))
{
System.out.println("That is correct! Harry was an only child.");
}
else if (answer1.equals("TRUE"))
{
System.out.println("That is correct! Harry was an only child.");
}
else if (answer1.equals("t"))
{
System.out.println("That is correct! Harry was an only child.");
}
else if (answer1.equals("T"))
{
System.out.println("That is correct! Harry was an only child.");
}
else
{
System.out.println("Sorry that is incorrect. Harry was an only child.");
}
System.out.println();
//Question 2
System.out.println ("T/F: Ron Weasley has more than one brother.");
String answer2 = input.next();
if (answer2.equals("True"))
{
System.out.println(" 100% correct! Ron has 2 twin brothers.");
}
else if (answer2.equals("true"))
{
System.out.println("100% correct! Ron has 2 twin brothers.");
}
else if (answer2.equals("TRUE"))
{
System.out.println("100% correct! Ron has 2 twin brothers.");
}
else if (answer2.equals("t"))
{
System.out.println("100% correct! Ron has 2 twin brothers.");
}
else if (answer2.equals("T"))
{
System.out.println ("100% correct! Ron has 2 twin brothers.");
}
else
{
System.out.println ("Sorry that is incorrect. Ron has 2 twin brothers.");
}
System.out.println();
//Question 3
System.out.println("What was the first name of Harry's uncle?");
System.out.println("a) Remus");
System.out.println("b) Snape");
System.out.println("c) Sirius");
String answer3 = input.next();
if (answer3.equals("a"))
{
System.out.println ("Sorry that is incorrect. ");
}
else if (answer3.equals ("A"))
{
System.out.println ("Sorry that is incorrect.");
}
else if (answer3.equals ("b"))
{
System.out.println ("Sorry that is incorrect. ");
}
else if (answer3.equals("B"))
{
System.out.println ("Sorry that is incorrect. ");
}
else if (answer3.equals ("C"))
{
System.out.println ("That is correct! ");
}
else if (answer3.equals ("c"))
{
System.out.println ("That is correct! ");
}
else
{
System.out.println ("THAT WASN'T EVEN AN OPTION YO. This is incorrect.");
}
System.out.println();
//question 4
System.out.println("What is the name of one of the houses at Hogwarts");
String answer4 = input.next();
if (answer4.equals("Gryffindor"))
{
System.out.println ("You are correct! Students from Gryffindor are known for their bravery.");
}
else if (answer4.equals("gryffindor"))
{
System.out.println("You are correct! Students from Gryffindor are known for their bravery.");
}
else if (answer4.equals("Slytherin"))
{
System.out.println( "You are correct! Slytherin are known for being cunning and ambitious.");
}
else if (answer4.equals("slytherin"))
{
System.out.println(" You are correct! Slytherins are known for being cunning and ambitious.");
}
else if (answer4.equals("Ravenclaw"))
{
System.out.println("You are correct! Ravenclaws are known for their intelligence.");
}
else if (answer4.equals("ravenclaw"))
{
System.out.println("You are correct! Ravenclaws are known for their intelligence.");
}
else if (answer4.equals ("Hufflepuff"))
{
System.out.println("You are correct! Hufflepuffs are known for their kindness.");
}
else if (answer4.equals ("hufflepuff"))
{
System.out.println("You are correct! Hufflepuffs are known for their kindess.");
}
else
{
System.out.println(" Sorry that is incorrect :(");
}
}
}
You can declare a variable at the top of your code, along the lines of
int correct = 0;
Then once it goes into one of your correct answers, update the variable by adding one to it, ie correct++;
Then at the bottom of the code, put in a new if
statement.
if (correct >= 3) {
//message here
} else {
//other message
}
You can also drastically shorten the code by using equalsIgnoreCase
and ||
(or)
For example:
if (answer1.equalsIgnoreCase("True") || answer1.equalsIgnoreCase("T")) {
System.out.println("That is correct! Harry was an only child.");
correct++;
} else {
System.out.println("Sorry that is incorrect. Harry was an only child.");
}
First of all, I recommend you use equalsIgnoreCase
method to compare Strings
so that you avoid the ugly multiple equals
. You can read more about it in the official documentation .
Also, to determine the number of correct answers, use a simple int
variable. For each correct response, increment it. At the end, make a decision based on the value of the final value of the counter.
This is the simplified version of your code:
Scanner input = new Scanner(System.in);
int correctAnswersCounter = 0;
System.out.println("If your answer is not one of the options, it will be considered incorrect.");
System.out.println();
//Question 1
System.out.println("T/F: Harry Potter was an only child.");
String answer1 = input.next();
// equalsIgnoreCase() works for all variations like 'true', 'TRUE', 'TrUe', 'truE' etc. for example
if (answer1.equalsIgnoreCase("true") || answer1.equalsIgnoreCase("t")) {
System.out.println("That is correct! Harry was an only child.");
correctAnswersCounter++;
} else {
System.out.println("Sorry that is incorrect. Harry was an only child.");
}
System.out.println();
//Question 2
System.out.println("T/F: Ron Weasley has more than one brother.");
String answer2 = input.next();
if (answer2.equalsIgnoreCase("true") || answer2.equalsIgnoreCase("t")) {
System.out.println(" 100% correct! Ron has 2 twin brothers.");
correctAnswersCounter++;
} else {
System.out.println("Sorry that is incorrect. Ron has 2 twin brothers.");
}
System.out.println();
//Question 3
System.out.println("What was the first name of Harry's uncle?");
System.out.println("a) Remus");
System.out.println("b) Snape");
System.out.println("c) Sirius");
String answer3 = input.next();
if (answer3.equalsIgnoreCase("a")) {
System.out.println("Sorry that is incorrect. ");
} else if (answer3.equalsIgnoreCase("b")) {
System.out.println("Sorry that is incorrect. ");
} else if (answer3.equalsIgnoreCase("c")) {
System.out.println("That is correct! ");
correctAnswersCounter++;
} else {
System.out.println("THAT WASN'T EVEN AN OPTION YO. This is incorrect.");
}
System.out.println();
//question 4
System.out.println("What is the name of one of the houses at Hogwarts");
String answer4 = input.next();
if (answer4.equalsIgnoreCase("Gryffindor")) {
System.out.println("You are correct! Students from Gryffindor are known for their bravery.");
correctAnswersCounter++;
} else if (answer4.equalsIgnoreCase("Slytherin")) {
System.out.println("You are correct! Slytherin are known for being cunning and ambitious.");
correctAnswersCounter++;
} else if (answer4.equals("Ravenclaw")) {
System.out.println("You are correct! Ravenclaws are known for their intelligence.");
correctAnswersCounter++;
} else if (answer4.equals("Hufflepuff")) {
System.out.println("You are correct! Hufflepuffs are known for their kindness.");
correctAnswersCounter++;
} else {
System.out.println(" Sorry that is incorrect :(");
}
// now make a decision based on the value of correctAnswersCounter
if(correctAnswersCounter > 3) {
System.out.println("Congrats!");
}
else {
System.out.println("Try harder next time!");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.