将数字标记映射到基于字母的标记

我正在尝试编写一个程序来接收一个数字,并使用 javascript 和 html 将其映射到基于字母等级的标记。


当我运行代码时,我没有得到任何答案。


这是代码:


HTML


   <h1>mapping a numerical grade to a letter grade</h1>

    </header>


    <div class="column1">

      <div class="input">

        a <input id="number1" type="number" min=" -32768" max=" 32767" />

      </div>

      <div class="input">

        <button onclick="mapping()">enter</button>

      </div>

      <strong><p id="output"></p></strong>

    </div>

Javascript


  function mapping() {


  var a = parseInt(document.getElementById("number1").value);


  switch (true) {

    case a > 89:

      answer = "A+";

      break;

    case a > 79:

      answer = "A";

      break;

    case a > 74:

      answer = "B+";

      break;

    case a > 69:

      answer = "B";

      break;

    case a > 64:

      answer = "C+";

      break;

    case a > 59:

      answer = "C";

    case a > 54:

      answer = "D+";

    case a > 49:

      answer = "D";

      break;

    case a > 40:

      answer = "E";

      break;


    default:

      answer = "F";


      document.getElementById("output").innerHTML = answer;

}


慕桂英4014372
浏览 137回答 2
2回答

慕标5832272

document.getElementById("output").innerHTML = answer;在 switch 语句中。default它只会被执行此代码将起作用:function mapping() {&nbsp; var a = parseInt(document.getElementById("number1").value);&nbsp; switch (true) {&nbsp; &nbsp; case a > 89:&nbsp; &nbsp; &nbsp; answer = "A+";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 79:&nbsp; &nbsp; &nbsp; answer = "A";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 74:&nbsp; &nbsp; &nbsp; answer = "B+";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 69:&nbsp; &nbsp; &nbsp; answer = "B";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 64:&nbsp; &nbsp; &nbsp; answer = "C+";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 59:&nbsp; &nbsp; &nbsp; answer = "C";&nbsp; &nbsp; case a > 54:&nbsp; &nbsp; &nbsp; answer = "D+";&nbsp; &nbsp; case a > 49:&nbsp; &nbsp; &nbsp; answer = "D";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case a > 40:&nbsp; &nbsp; &nbsp; answer = "E";&nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; answer = "F";&nbsp; }&nbsp; document.getElementById("output").innerHTML = answer;}

GCT1015

开关(真)不起作用。开关 (1) 可以。此外,我认为 case 只接受值,而不接受比较——至少当我在 Chrome 和 Opera 上尝试时是这样。所以您可能想使用 if-else 语句重写它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript