Javascript 值一起显示,而不是作为新值显示
我今天在大学里做了一个练习,它是一个JavaScript程序,用来计算学生在考试中的平均分数 这是我的密码:Javascript 值一起显示,而不是作为新值显示,javascript,Javascript,我今天在大学里做了一个练习,它是一个JavaScript程序,用来计算学生在考试中的平均分数 这是我的密码: <!DOCtype html> <html> <head> <title>While loop</title> </head> <body> <script> //The total score of all pupils var total = 0; //The
<!DOCtype html>
<html>
<head>
<title>While loop</title>
</head>
<body>
<script>
//The total score of all pupils
var total = 0;
//The number of scores
var count = 1;
while (count <= 10) {
grade = prompt("Insert the grade:");
total = total + grade;
count++;
}
var average = +total / 10;
document.write("The average is " + average);
</script>
</body>
</html>
While循环
//所有学生的总分
var合计=0;
//分数
var计数=1;
而(countgrade=prompt(“Insert the grade:”);
则是问题所在。prompt将您的输入作为字符串,而在JS中,添加两个字符串只是将值串联起来。因此,分析您的输入:
grade = +prompt("Insert the grade:"); //+ is shorthand for casting to a number
或者使用parseInt
grade = prompt("Insert the grade:");
var numberGrade = parseInt(grade);
仅供参考-您要添加的所有数字都必须是整数-否则将再次以字符串结束,例如:
10 + 10; //20
10 + "10" //1010
10 + 10 + "10" //2010
grade=prompt(“插入分数:”);
是问题所在。提示将您的输入作为字符串,在JS中,添加两个字符串只是将值串联起来。因此,分析您的输入:
grade = +prompt("Insert the grade:"); //+ is shorthand for casting to a number
或者使用parseInt
grade = prompt("Insert the grade:");
var numberGrade = parseInt(grade);
仅供参考-您要添加的所有数字都必须是整数-否则将再次以字符串结束,例如:
10 + 10; //20
10 + "10" //1010
10 + 10 + "10" //2010
改变
total=total+grade;
到
total=total+parseInt(年级);
编写total=total+grade
js时,将total和grade连接为字符串更改
total=total+grade;
到
total=total+parseInt(年级);
<>太长了,读不下去了,太长了,读不下去了。< > >当你写代码>总数=总数+等级>代码> JS将字符串的总数和等级作为T/L的可能复制。Javascript博士不是强类型的,它将根据你是如何使用它来将变量关联到它认为适合的任何类型。<代码> + /代码>是字符串的CopAT,但是要添加整数。TL的可能副本;Javascri博士。pt不是强类型,它会根据您的使用方式将变量强制转换为它认为合适的任何类型。+
是concat表示字符串,而add表示整数。谢谢,这个答案比这个问题的答案更容易理解。:)谢谢,这个答案比这个问题的答案更容易理解。:)