我在让我的代码通过以下if语句运行时遇到问题。我正在上在线课程,唯一的资源就是教科书。感谢帮助。我能够使它返回除提示,错误消息之外的所有内容并退出程序。它正确地运行一个连续循环。
var numberGrade;
var grade = [];
do {
numberGrade = prompt("Enter number grade from 0 through 100\nOr enter -1 to end entries\n");
numberGrade = parseInt(numberGrade);
if (numberGrade >= 0 && numberGrade <= 100 ) {
grade[grade.length] = numberGrade;
} else if (numberGrade != -1) {
alert("Entry not valid!\n Entry must be a valid number from 0 through 100 \n Or enter -1 to end entries");
}
}
while (numberGrade != -1);
for (var num = 0; num <= grade; num++) {
if (grade >= 0 && grade <= 59) {
alert ("Number grade =" + grade + "\nLetter Grade = F");
}
if (grade >= 60 && grade <= 62) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = D-");
}
if (grade >= 63 && grade <= 66) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = D");
}
if (numberGrade >= 67 && numberGrade <= 69) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = D+");
}
if (numberGrade >= 70 && numberGrade <= 76){
alert ("Number grade =" + numberGrade + "\nLetter Grade = C");
}
if (numberGrade >= 77 && numberGrade <= 79) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = C+");
}
if (numberGrade >= 80 && numberGrade <= 82) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = B-");
}
if (numberGrade >= 83 && numberGrade <= 86) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = B");
}
else if (numberGrade >= 87 && numberGrade <= 89) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = B+");
}
if (numberGrade >= 90 && numberGrade <= 92) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = A-");
}
if (numberGrade >= 93 && numberGrade <= 100) {
alert ("Number grade =" + numberGrade + "\nLetter Grade = A");
}
}
答案 0 :(得分:2)
在循环初始化中,语句num <= grade
是没有意义的,因为grade
是数组而不是数字。将循环更改为for (var num = 0; num < grade.length; num++)
。然后,您的if
语句应针对grade
数组的元素进行测试:
if (grade[num] >= 0 && grade[num] <= 59) {
alert ("Number grade =" + grade[num] + "\nLetter Grade = F");
}
答案 1 :(得分:0)
我对您的代码做了一些改动以使其正常工作。可以用更好的方法来完成它,但是我选择保持简单,以便您仍然可以理解它。
我创建了一个将数字等级转换为字母的函数,返回要输出的字符串。
var numberGrade;
do {
numberGrade = prompt("Enter number grade from 0 through 100\nOr enter -1 to end entries\n");
numberGrade = parseInt(numberGrade);
if (numberGrade >= 0 && numberGrade <= 100 ) {
// If the input is valid, calls the function and creates an alert with the return
alert(convertGrade(numberGrade));
} else if (numberGrade != -1) {
alert("Entry not valid!\n Entry must be a valid number from 0 through 100 \n Or enter -1 to end entries");
}
} while (numberGrade != -1);
// Function that converts the grade, returning the message
function convertGrade(grade) {
if (grade >= 0 && grade <= 59) {
return ("Number grade =" + grade + "\nLetter Grade = F");
}
if (grade >= 60 && grade <= 62) {
return ("Number grade =" + grade + "\nLetter Grade = D-");
}
if (grade >= 63 && grade <= 66) {
return ("Number grade =" + grade + "\nLetter Grade = D");
}
if (grade>= 67 && grade <= 69) {
return ("Number grade =" + grade + "\nLetter Grade = D+");
}
if (grade>= 70 && grade <= 76){
return ("Number grade =" + grade + "\nLetter Grade = C");
}
if (grade>= 77 && grade <= 79) {
return ("Number grade =" + grade + "\nLetter Grade = C+");
}
if (grade>= 80 && grade <= 82) {
return ("Number grade =" + grade + "\nLetter Grade = B-");
}
if (grade>= 83 && grade <= 86) {
return ("Number grade =" + grade + "\nLetter Grade = B");
}
if (grade>= 87 && grade <= 89) {
return ("Number grade =" + grade + "\nLetter Grade = B+");
}
if (grade>= 90 && grade <= 92) {
return ("Number grade =" + grade + "\nLetter Grade = A-");
}
if (grade>= 93 && grade <= 100) {
return ("Number grade =" + grade + "\nLetter Grade = A");
}
}