【问题标题】:How to keep score for questions answered correct and deduct points for questions answered wrong?回答正确的问题如何记分,回答错误的问题如何扣分?
【发布时间】:2019-07-12 04:47:50
【问题描述】:

使用 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】:

    您可以添加一个变量保持分数。加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);

    【讨论】:

    • 这有帮助,谢谢哥们。讨厌问愚蠢的问题,但我是这里的菜鸟,所以感谢所有帮助。
    • @Designird 当然。我总是很乐意提供帮助。如果它对您有用,请确保接受答案,让其他人知道它也有效。没有愚蠢的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多