Java 使用关系运算符查找分数
因此,对于我代码的最后一部分,我试图根据用户正确回答的问题数量做出评论。如果用户得到了3个或更多的正确答案,我想留下一个祝贺。但是如果用户得到的少于这个,我想留下一条评论,以便进一步研究。我知道我应该使用关系运算符来解决这个问题,但我有点困惑于我到底是如何做到的Java 使用关系运算符查找分数,java,Java,因此,对于我代码的最后一部分,我试图根据用户正确回答的问题数量做出评论。如果用户得到了3个或更多的正确答案,我想留下一个祝贺。但是如果用户得到的少于这个,我想留下一条评论,以便进一步研究。我知道我应该使用关系运算符来解决这个问题,但我有点困惑于我到底是如何做到的 import java.util.Scanner; public class quizcreation { public static void main (String [] args) { Scanner in
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 :(");
}
}
}
您可以在代码顶部声明一个变量,如下所示:
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.");
}
首先,我建议您使用equalsIgnoreCase
方法来比较字符串
,以避免出现丑陋的倍数equals
。你可以阅读更多关于它的内容
此外,要确定正确答案的数量,请使用简单的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!");
}
使用equalsIgnoreCase
而不是equals,并使用|
条件使代码更好。注意,您可能还应该使用input.nextLine()
而不是input.next()
,以避免意外地同时回答两个问题(尝试输入t
)在第一个问题上看我的意思)。另一种检查答案的方法是Arrays.asList(“TRUE”,“T”).contains(input.toUppercase())
——好的地方是,创建一个“question”类更容易,从而进一步缩减代码。