gpt4 book ai didi

javascript - 当我想打印成绩时我的 java 脚本出错

转载 作者:行者123 更新时间:2023-11-30 21:09:16 24 4
gpt4 key购买 nike

当我只打印出学生姓名和他们的分数时,我没有遇到任何问题,但是当我编写用于计算或返回他们的成绩的代码时,它没有显示我想要的成绩。当我想输入时学生 1 的学生姓名和分数,它只是跳过学生 2 并询问学生 3 的姓名和年级...有人知道如何解决吗?下面是我的代码:

function Average() {
var text = "";
var person = new Array();
var marks = new Array();
var grade = new Array();
for (var i = 0; i < 3; i++) {
person[i] = window.prompt("enter student name " + (i + 1) + "of 3", "");
marks[i] = window.prompt("enter student marks " + (i + 1) + "of 3", "");

for (var i = 0; i < marks.length; i++) {
if (marks[i] >= 85 && marks[i] < 100) {
grade[i] = "HD";
} else if (marks[i] >= 75 && marks[i] < 85) {
grade[i] = "D";
} else if (marks[i] >= 65 && marks[i] < 75) {
grade[i] = "C";
} else if (marks[i] >= 50 && marks[i] < 35) {
grade[i] = "P";
} else if (marks[i] >= 00 && marks[i] < 50) {
grade[i] = "F";
}

text += "the student name is : " + person[i] + "&nbsp" + "&nbsp" + "theirs marks are : " + marks[i] + grade[i] + "<br>";
}
// text += "the student name is : " + person[i] +"&nbsp" +"&nbsp" + "theirs marks are : " + marks[i] + grade[i] +"<br>";
}
document.getElementById("id").innerHTML = text;
}
<p> click this to active the window prompt </p>
<button onclick="Average()"> Click Me </button>
<p id="id"></p>

我的结果:

enter image description here

最佳答案

在开始循环计算之前,您还没有结束循环提示。它应该是:

function Average() {
var text = "";
var person = new Array();
var marks = new Array();
var grade = new Array();
for (var i = 0; i < 3; i++) {
person[i] = window.prompt("enter student name " + (i + 1) + "of 3", "");
marks[i] = window.prompt("enter student marks " + (i + 1) + "of 3", "");
}
for (var i = 0; i < marks.length; i++) {
if (marks[i] >= 85 && marks[i] < 100) {
grade[i] = "HD";
} else if (marks[i] >= 75 && marks[i] < 85) {
grade[i] = "D";
} else if (marks[i] >= 65 && marks[i] < 75) {
grade[i] = "C";
} else if (marks[i] >= 50 && marks[i] < 35) {
grade[i] = "P";
} else if (marks[i] >= 00 && marks[i] < 50) {
grade[i] = "F";
}

text += "the student name is : " + person[i] + "&nbsp" + "&nbsp" + "theirs marks are : " + marks[i] + grade[i] + "<br>";
}
document.getElementById("id").innerHTML = text;
}
<p> click this to active the window prompt </p>
<button onclick="Average()"> Click Me </button>
<p id="id"></p>

此外,您的评分系统不计算 100(只有 <100),我认为您输入的是“35”而不是“65”:

if (marks[i] >= 85 && marks[i] <= 100) {
...
} else if (marks[i] >= 50 && marks[i] < 65) {

希望这对您有所帮助!

关于javascript - 当我想打印成绩时我的 java 脚本出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46301669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com