如何保持正确答案的分数,并为错误的问题扣除积分?

问题描述 投票:0回答:1

使用do while循环,我正在尝试为正确回答的问题保留分数,并且想要针对回答错误的问题扣除积分。我确定有办法做到这一点,我想我只需要一些帮助。

var answer1 = "Batman";
var answer = false;
do {
   var guess = prompt("Who is knows as the Caped Crusader?");
    if (guess == answer1) {
      alert("Yes, you got it right! The Caped Crusader is known as " + answer1 + " You get 1 point!");
      answer = true;
    }
  else {
    alert("Sorry, wrong hero. Try again!");
  }
}
while (answer == false);
javascript loops input output do-while
1个回答
0
投票

您只需添加变量保持分数即可。添加1以添加点并减去1以扣除点数。

var answer1 = "Batman";
var answer = false;
var score = 0;

do {
  var guess = prompt("Who is knows as the Caped Crusader?");
  if (guess == answer1) {
    alert("Yes, you got it right! The Caped Crusader is known as " + answer1 + " You get 1 point!");
    score++;
    answer = true;
  } else {
    alert("Sorry, wrong hero. Try again!");
    score--;
  }
}
while (answer == false);

alert("Your score is: " + score);
© www.soinside.com 2019 - 2024. All rights reserved.