因此,对于我的代码的最后一部分,我试图根据用户正确的问题数量发表评论。如果用户有3个或更多正确,我想留下恭喜。但是如果用户得到的比较少,我想留下评论来研究一下。我知道我应该使用关系运算符来解决这个问题,但我对如何实际操作感到困惑。
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 :(");
}
}
}
答案 0 :(得分:1)
您可以在
行的代码顶部声明一个变量int correct = 0;
然后,一旦进入您的正确答案之一,请通过向其添加一个来更新变量,即correct++;
然后在代码的底部,输入一个新的if
语句。
if (correct >= 3) {
//message here
} else {
//other message
}
您还可以使用equalsIgnoreCase
和||
(或)
例如:
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.");
}
答案 1 :(得分:0)
首先,我建议您使用equalsIgnoreCase
方法比较Strings
,以避免丑陋的多重equals
。您可以阅读更多相关信息in the official documentation。
此外,要确定正确答案的数量,请使用简单的int
变量。对于每个正确的响应,递增它。最后,根据计数器最终值的值做出决定。
这是您的代码的简化版本:
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!");
}