如何将这些分数相加得出最终值?
这是我的第一个代码块,我的主类。我已经创建了问题数组和对象,这只是为了向你们展示我想要完成的事情。
for(int i = 0; i<questionArray.length; i++)
{
questionArray[i].askQuestion();
questionArray[i].getAnswer();
questionArray[i].checkAnswer();
}
这些是我在上面的循环中使用的方法:
public void askQuestion()
{
System.out.println(question);
System.out.println("This question is worth " + pointValue + " point(s)");
System.out.println("Please enter your answer: ");
}
public String getAnswer()
{
return answer;
}
public boolean checkAnswer()
{
Scanner keyboard = new Scanner(System.in);
String UserAnswer = keyboard.nextLine();
if (UserAnswer.equalsIgnoreCase(answer))
{
System.out.println("congratulations you have earned " + pointValue + " point(s)\n");
return true;
}
else
{
System.out.println("sorry you did not get the answer correct");
System.out.println("The correct answer is " + this.answer + "\n");
return false;
}
}
正如您所看到的,这是一个 TriviaGame
类, 1.)我试图制作两种方法,一种能够在每个问题后显示pointValue(仅当用户答案==答案时)。 2.)第二个是当用户得到正确答案时将每个pointValue相加(同样当答案相等时。又名checkAnswer == true),并在最后显示所有结果。
This is my first block of code, my main class. I already have the question array and objects created, this is just to show you guys what I am trying to get done.
for(int i = 0; i<questionArray.length; i++)
{
questionArray[i].askQuestion();
questionArray[i].getAnswer();
questionArray[i].checkAnswer();
}
These are my methods used in the loop above:
public void askQuestion()
{
System.out.println(question);
System.out.println("This question is worth " + pointValue + " point(s)");
System.out.println("Please enter your answer: ");
}
public String getAnswer()
{
return answer;
}
public boolean checkAnswer()
{
Scanner keyboard = new Scanner(System.in);
String UserAnswer = keyboard.nextLine();
if (UserAnswer.equalsIgnoreCase(answer))
{
System.out.println("congratulations you have earned " + pointValue + " point(s)\n");
return true;
}
else
{
System.out.println("sorry you did not get the answer correct");
System.out.println("The correct answer is " + this.answer + "\n");
return false;
}
}
So as you can see this is a TriviaGame
class,
1.)I am trying to make two methods, one that is capable of displaying the pointValue(only when user answer == answer) after each question.
2.)The second one is adding up each of the pointValue(again when answers are equal. A.k.a checkAnswer == true) when the user gets the answer correct, and display all the results at the end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的意思是:
顺便说一句,考虑将
if (checkAnswer() == true)
更改为if (checkAnswer())
did you mean:
As an aside, consider changing
if (checkAnswer() == true)
toif (checkAnswer())
在第一个代码块中,您在 for 循环中运行每个方法,您应该运行
questionArray[i].updateScore()
而不是questionArray[i].checkAnswer() .截至目前,您正在运行
checkAnswer()
,它会从用户那里获取答案并检查其是否正确。但如果你打算在此之后调用updateScore()
,我相信你最终会在 if 语句的条件下再次运行checkAnswer()
,从而收集答案用户无意中再次出现。通过运行updateScore()
,您将收集答案并更新分数。我不确定你是否已经知道这一点,但我想我会让你知道以防万一。至于你最初的问题,你可以试试这个:
为您的 Question 类创建一个布尔实例变量(初始化为 false),如果答案正确,该变量将设置为 true。这样,您就可以遍历您的
questionArray
并检查他们的答案是否正确。如果回答正确,请将问题的pointValue
添加到您的总分中。我希望我理解你的问题,希望这有帮助,
泰勒
In your first block of code, where you run each method within the for loop, you should run
questionArray[i].updateScore()
instead ofquestionArray[i].checkAnswer()
. As of right now, you are runningcheckAnswer()
which gets an answer from the user and checks to see whether it is correct. But if you plan to callupdateScore()
after this, I believe you will end up runningcheckAnswer()
again within the condition of the if statement, thus collecting an answer from the user again without meaning to. By running justupdateScore()
, you will both gather the answer AND update the score. I'm not sure if you knew this already, but I thought I'd let you know just in case.As for your initial question, you could try this:
Create an boolean instance variable for your Question class (initialized to false) that will be set to true if their answer was correct. This way, you can iterate through your
questionArray
and just check to see whether their answer was correct or not. If it was answered correctly, then add the Question'spointValue
to your total score.I hope I understood your question and I hope this helps,
Tyler
最简单的方法就是使用实例或静态变量来跟踪它。
The easiest and simplest way to do this is to just use a instance or static variable to keep track of it.
您想使用变量
score
来汇总迄今为止获得的积分吗?然后你必须将其设为类的属性或将其声明为静态,应将其初始化为 0,事实上,必须添加每次获得的分数,正如 Bohemian 已经告诉你的那样。以及
updateScore 方法的本地值,当该方法离开时,它的值将会丢失。如果你宣布
Rgds,
托马斯
Do you want to use the variable
score
to aggregate the points earned so far? Then you have to make it an attribute of the class or declare it as static, should initialize it with 0 and, in fact, have to add the score earned each time, as Bohemian already told you.as well as
is local to your updateScore method and it's value will be lost when the method is left. If you declare
Rgds,
Thomas