无法在java脚本中打印switch case的结果

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

我输入了一个 om(获得的分数),另一个输入了 mm(总分),我正在尝试计算百分比和等级。我得到了百分比分数的输出,但在编写 switch case 后无法得到成绩的输出。请检查代码并让我知道我的错误。

function perc() { 
  document.getElementById("perc").value=            (document.getElementById("om").value/document.getElementById("mm").value)*100

  var percentage=document.getElementById("perc").value
  var grade;

  switch(Math.floor(percentage/10)) {
    case 9:grade="outstanding"
      break;
    case 8:grade="very good"
      break;
    case 7:grade="good"
      break;
    case 6:grade="improve well"
      break;
    case 5:grade="average"
      break;
    default:grade="fail"
      break;
  }

  document.getElementById("grade").value=grade;
<h1>percentage & grade calculator</h1>
MaxMarks:-<input type="text" id="mm"><br><br>
obtained marks:-<input type="text" id="om"><br><br>
<button type="button" onclick="perc()">SUBMIT</button><br><br>
percentage:-<input type:-"text" id="perc"><br><br>
Grade:-<input type="text" id="grade"><br><br>

起初,在我编写了年级代码后,我得到了百分比的输出,但我没有得到任何输出

javascript html input switch-statement output
1个回答
0
投票

perc
功能未关闭。 您需要关闭函数的大括号。 试试这个

<html>

<head>
       <title>percentage</title>
</head>
<script>

       function perc() {

              document.getElementById("perc").value = (document.getElementById("om").value / document.getElementById("mm").value) * 100

              var percentage = document.getElementById("perc").value

              var grade;

              switch (Math.floor(percentage / 10)) {
                     case 9: grade = "outstanding"
                            break;
                     case 8: grade = "very good"
                            break;
                     case 7: grade = "good"
                            break;
                     case 6: grade = "improve well"
                            break;
                     case 5: grade = "average"
                            break;
                     default: grade = "fail"
                            break;
              }

              document.getElementById("grade").value = grade;
       } // This is the missing bracket
</script>

<body>
       <h1>percentage & grade calculator</h1>

       MaxMarks:-<input type="text" id="mm"><br><br>

       obtained marks:-<input type="text" id="om"><br><br>

       <button type="button" onclick="perc()">SUBMIT</button><br><br>

       percentage:-<input type:-"text" id="perc"><br><br>

       Grade:-<input type="text" id="grade"><br><br>

</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.