如何将这些分数相加得出最终值?

发布于 2024-12-17 07:18:28 字数 1157 浏览 0 评论 0原文

这是我的第一个代码块,我的主类。我已经创建了问题数组和对象,这只是为了向你们展示我想要完成的事情。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

入怼 2024-12-24 07:18:28

您的意思是:

 int score += pointValue; // note the plus (+)

顺便说一句,考虑将 if (checkAnswer() == true) 更改为 if (checkAnswer())

did you mean:

 int score += pointValue; // note the plus (+)

As an aside, consider changing if (checkAnswer() == true) to if (checkAnswer())

但可醉心 2024-12-24 07:18:28

在第一个代码块中,您在 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 of questionArray[i].checkAnswer() . As of right now, you are running checkAnswer() which gets an answer from the user and checks to see whether it is correct. But if you plan to call updateScore() after this, I believe you will end up running checkAnswer() again within the condition of the if statement, thus collecting an answer from the user again without meaning to. By running just updateScore() , 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's pointValue to your total score.

I hope I understood your question and I hope this helps,
Tyler

万劫不复 2024-12-24 07:18:28

最简单的方法就是使用实例或静态变量来跟踪它。

The easiest and simplest way to do this is to just use a instance or static variable to keep track of it.

你与清晨阳光 2024-12-24 07:18:28

您想使用变量 score 来汇总迄今为止获得的积分吗?然后你必须将其设为类的属性或将其声明为静态,应将其初始化为 0,事实上,必须添加每次获得的分数,正如 Bohemian 已经告诉你的那样。

int score = pointValue;

以及

int score += pointValue; 

updateScore 方法的本地值,当该方法离开时,它的值将会丢失。如果你宣布

static int score = 0;

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.

int score = pointValue;

as well as

int score += pointValue; 

is local to your updateScore method and it's value will be lost when the method is left. If you declare

static int score = 0;

Rgds,

Thomas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文