1

我正在制作一个程序,教师输入学生的姓名和与一定数量的作业相对应的成绩(教师指定作业)。

我想让它成为输入成绩的数字值。所以,我添加了这段代码:

for (var g = 1; g <= assignments; g++) {
     var grade = prompt("Please enter the student score for assignment" + g + ".");
     if(typeof grade === 'number' && Math.Round(grade) % 1 == 0) {
             return true;
         } 
     else{ 
             var grade = prompt("Please enter the student score for assignment" + g + ".");

         }

         var gradecolumn = document.createElement("td");
         var gradetext = document.createTextNode(grade);
         gradecolumn.appendChild(gradetext);
         rowtwo.appendChild(gradecolumn);
    }

我把它记下来了......但我想让它继续提示,直到用户输入有效的数据类型。

我遇到的另一个问题是我想在输入成绩后将成绩加在一起并计算累积成绩以在某些评分等级上分配字母成绩...我知道我将通过多条件 if/ 分配字母成绩else if 语句。但我不确定在输入成绩后如何调用成绩,以及如何将它们加在一起,以便我可以将结果输入到多条件部分。

任何帮助将不胜感激..另外,我应该使用 parseInt 吗?

4

2 回答 2

1
var range = {"A": 100, "B": 75, "C": 50, "D": 30, "E": 20, "F": 10}, score = 0;
for (var g = 1; g <= 3; g++) {
    var grade = undefined, letter = '';
    while (!grade) {
        var grade = prompt("Please enter the student score for assignment" + g + ".");
        if (parseInt(grade)) {
            // accumulate score
            score += parseInt(grade);
        }
        // grade is not a number
        else grade = undefined;
    }
}
// check for mark
for (key in range) {
    if (score<range[key]) letter = key;
}

if (!letter) alert("score "+score+" is out of range!");
else {
    alert(letter); // now you have it
}

演示也更新了:http: //jsfiddle.net/vladkras/jNg2m/2/

于 2013-06-29T03:18:08.977 回答
0

尝试这个:

var g = 1;
var assignments = 5;
var done = false;
var grade, gradecolumn, gradetext;

while (!done)
{
    grade = prompt("Please enter the student score for assignment " + g + ".");
    if((grade+'').search(/^[0-9]{1,3}$/) > -1) 
    {
        gradecolumn = document.createElement("td");
        gradetext = document.createTextNode(grade);
         gradecolumn.appendChild(gradetext);
         rowtwo.appendChild(gradecolumn);

         // increment the counter
         g++;
         if (g > assignments)
         {
            done = true;
         }
    }
    else
    {
        alert('Please enter a valid score for assignment '+g+'.');
    }
}
于 2013-06-29T03:22:32.803 回答