有谁知道为什么我的"号码"增量计数器中的变量(for(p = 0; p< number; p = p + 1))不识别整数(它位于totalscore函数)?它打算打印出来在打印出3-5个随机数之后,我打算将这些值存储到一个数组中并使用数组排序对它们进行排序。但问题是所有3 - 3个随机数而不是1个随机数。 5个随机值存储在一个变量中。任何有用的建议都会非常感激。抱歉,如果我的代码很乱,我还是初学者,我希望我的解释足够让你理解
< HTML>
< !Foundation Page for building our Javascript programs>
< HEAD>
< TITLE>The Foundation Page
< /TITLE>
<SCRIPT LANGUAGE="JavaScript">
var number;
function main() {
start()
totalscore()
leaderboard()
}
function start() {
var ask
var name1
var name2
var name3
var name4
var name5
ask = prompt("How many people are playing")
if (ask == 3) {
name1 = prompt("What is the first player's name?")
name2 = prompt("What is the second player's name?")
name3 = prompt("What is the third player's name?")
number = 3
}
if (ask == 4) {
name1 = prompt("What is the first player's name?")
name2 = prompt("What is the second player's name?")
name3 = prompt("What is the third player's name?")
name4 = prompt("What is the forth player's name?")
number = 4
}
if (ask == 5) {
name1 = prompt("What is the first player's name?")
name2 = prompt("What is the second player's name?")
name3 = prompt("What is the third player's name?")
name4 = prompt("What is the forth player's name?")
name5 = prompt("What is the fifth player's name?")
number = 5
}
}
function randomnumber() {
var randomnumber;
randomnumber = Math.random() * 3;
return (Math.floor(randomnumber + 0.2));
}
function totalscore() {
var n;
var p;
var score = 0;
var total;
for (p = 0; p < number; p = p + 1) {
total = 0
for (n = 0; n < 10; n = n + 1) {
number = randomnumber();
if (number == 0) {
score = score + 0;
} else if (number == 2) {
score = score + 2;
} else if (number == 3) {
score = score + 3;
}
total = total + score;
}
document.write("These's player will have the following score")
document.write(total + "<br>")
}
}
function leaderboard() {
var leader = new array(5);
leader[0] =
leader[1] =
leader[2] =
leader[3] =
leader[4] =
}
</SCRIPT>
< HEAD>
< BODY>
< BODY BGCOLOUR="WHITE">
< H2>The Foundation Page
< /H2>
<HR>
<SCRIPT LANGUAGE="Javascript">
main()
</SCRIPT>
< /BODY>
</HTML>
&#13;
答案 0 :(得分:0)
首先,您应该采用一些代码标准:目前您的代码看起来非常有意义,所以它无法帮助其他人查看它...甚至它对您没有帮助!
所以你应该考虑以下几点:
<html>
而不是< HTML >
< !Foundation Page for building our Javascript programs >
之类的评论,而是<!-- Foundation Page for building our Javascript programs -->
<SCRIPT LANGUAGE = "JavaScript">
中的过时属性:只需编写<script>
</head>
代码(目前已写入< HEAD >
)< BODY BGCOLOUR = "WHITE" >
:使用CSS,在body {background-color: white;}
元素或.css文件中编写<style>
之类的内容<body>
代码无意义a=b+c;
写a = b + c;
之类的内容,但不要混用这两种表达式现在关于你的问题:除了上面列出的项目之外,即使只是简单一瞥,也有一些原因导致这个代码不能正常工作,只有你发布的那个。
然后,由于您报告的问题类似于“number
变量未被识别或未获得预期值”,这意味着您的代码工作的状态与您发布的不同(或者您使用的是特别宽容的浏览器,我很想知道:-)。
请在与您的问题相关的问题状态下发布代码!
在任何情况下,这里都有一些要点我建议审查:
<script>
,位于/body>
之前,同时包含您的功能和main()
来电main()
,它本身调用其他函数,而不是直接将那些其他调用作为脚本体leaderbord()
功能目前尚未实现,但从未保留过它的特征,或整个应用程序无效;至少全球评论nameX
函数退出时,你所有的start()
变量都是无用的number
变量的问题:
ask()
totalscore()
函数中,您将此值用于循环使用此数量的用户,但也会使用从randomnumber()
函数获取的值覆盖它:显然它不能驱动循环过程了!在这里,您必须使用不同的(本地)变量来保存随机值ask()
函数(查看下面的建议代码);当number
不是3,4或5,甚至是空或不是数字时,程序应该做什么也不足randomnumber()
功能无用,因为它只使用一次而不包含一些复杂的代码这是根据上面提到的项目增强的代码建议,以及其他一些小的。但请注意,它仍然远非真正的好,不仅在编码风格的角度,而且在战略和效率方面。
<html>
<!-- Foundation Page for building our Javascript programs -->
<head>
<title>The Foundation Page</title>
<style>
body {
background-color: white;
}
</style>
<script>
var number,
names = start(); // so names can be used further
totalscore();
leaderboard();
function start() {
var names = [];
number = prompt ("How many people are playing");
for (var i = 0; i < number; i++) {
names[i] = prompt("What is the player " + i + "'s name?");
}
return names;
}
function totalscore() {
var total,
score = 0;
for(var p = 0; p < number; p++) {
total = 0;
for (var n = 0; n < 10; n++) {
var randomvalue = Math.floor(Math.random() * 3 + 0.2);
if (randomvalue == 0) {
score = score + 0;
}
else if (randomvalue == 2) {
score = score + 2;
}
else if (randomvalue == 3) {
score = score + 3;
}
total = total + score;
}
document.write ("These's player will have the following score: ");
document.write( total + "<br />");
}
}
function leaderboard () {
/*
var leader = new array(5);
leader[0]=
leader[1]=
leader[2]=
leader[3]=
leader[4]=
*/
}
</script>
</head>
<body>
<h2> The Foundation Page </h2>
<hr />
</body>
</html>