[英]Using relational operators to find the score
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.
如果用户得到3个或更多正确答案,我想表示祝贺。 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++;
然后,一旦它进入您的正确答案之一,则通过向其添加变量来更新变量,即
correct++;
Then at the bottom of the code, put in a new if
statement. 然后在代码底部,放入一个新的
if
语句。
if (correct >= 3) {
//message here
} else {
//other message
}
You can also drastically shorten the code by using equalsIgnoreCase
and ||
您还可以使用
equalsIgnoreCase
和||
大大缩短代码。 (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
. 首先,我建议您使用
equalsIgnoreCase
方法比较Strings
以便避免使用丑陋的equals
。 You can read more about it in the official documentation . 您可以在官方文档中了解更多信息。
Also, to determine the number of correct answers, use a simple int
variable. 另外,要确定正确答案的数量,请使用一个简单的
int
变量。 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!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.