代码如下:
function studentName(x)
{
while(x == '')
{
x = prompt('Cannot leave field blank. Enter again');
}
return(x)
}
function studentScore(y)
{
while(y == '' || y > 100 || y < 0 || isNaN(y))
{
y = parseFloat(prompt('Invalid score. Enter score again'));
}
return(y)
}
function another(z,t)
{
while(z == '' && z != 'n' && z != 'N' && z != 'y' && z != 'Y')
{
if(z == 'y' || z == 'Y')
{
z = prompt('Invalid Option. Enter another score? Y or N')
}
else
{
t = prompt('Invalid Option. Enter another student? Y or N')
}
}
return(z)
}
var names = []
var scores = []
var redo = true
var anotherName
var redo2
var retry = true
var anotherScore
var retry2
var i = 0
var a = 1
while(redo == true)
{
var studentNames = prompt('Enter student name');
var name = studentName(studentNames);
names.push(name)
while(retry == true)
{
var studentScores = parseFloat(prompt('Enter student score'));
var score = score + studentScore(studentScores);
retry = prompt('Enter another score? Y/N');
retry2 = another(retry);
if(retry == 'y' || retry == 'Y')
{
retry = true
a++
}
else if(retry == 'n' || retry == 'N')
{
retry = false
}
}
score = score / a
scores[i] = score
redo = prompt('Enter another student? Y/N');
redo2 = another(redo);
if(redo == 'y' || redo == 'Y')
{
redo = true
retry = true
i++;
a = 1
score = 0
}
else if(redo == 'n' || redo == 'N')
{
redo = false
}
}
for(y=0; y < names.length; y++)
{
alert(names[y] + " - " + scores[y]);
}
试着运行代码来理解这是一个很难描述的问题,但基本上,我认为发生的问题是数组没有正确存储第一个分数,因为它一直说学生的名字是名字,而分数要么是NaN,要么是未定义的
发布于 2020-01-13 05:13:12
请在循环外初始化score,并从循环中删除var关键字。
<script>
function studentName(x)
{
while(x == '')
{
x = prompt('Cannot leave field blank. Enter again');
}
return(x)
}
function studentScore(y)
{
while(y == '' || y > 100 || y < 0 || isNaN(y))
{
y = parseFloat(prompt('Invalid score. Enter score again'));
}
return(y)
}
function another(z,t)
{
while(z == '' && z != 'n' && z != 'N' && z != 'y' && z != 'Y')
{
if(z == 'y' || z == 'Y')
{
z = prompt('Invalid Option. Enter another score? Y or N')
}
else
{
t = prompt('Invalid Option. Enter another student? Y or N')
}
}
return(z)
}
var names = []
var scores = []
var redo = true
var anotherName
var redo2
var retry = true
var anotherScore
var retry2
var i = 0
var a = 1
while(redo == true)
{
var studentNames = prompt('Enter student name');
var name = studentName(studentNames);
names.push(name)
var score = 0;
while(retry == true)
{
var studentScores = parseFloat(prompt('Enter student score'));
score = score + studentScore(studentScores);
retry = prompt('Enter another score? Y/N');
retry2 = another(retry);
if(retry == 'y' || retry == 'Y')
{
retry = true
a++
}
else if(retry == 'n' || retry == 'N')
{
retry = false
}
}
score = score / a
scores[i] = score
redo = prompt('Enter another student? Y/N');
redo2 = another(redo);
if(redo == 'y' || redo == 'Y')
{
redo = true
retry = true
i++;
a = 1
score = 0
}
else if(redo == 'n' || redo == 'N')
{
redo = false
}
}
for(y=0; y < names.length; y++)
{
alert(names[y] + " - " + scores[y]);
}
</script>
发布于 2020-01-13 05:15:53
当您的循环第一次运行这行代码时:
var score = score + studentScore(studentScores);变量score尚未定义,因此上面的行将生成一个NaN。
要解决这个问题,只需在循环之前定义score:
var score = 0;https://stackoverflow.com/questions/59708107
复制相似问题