Does anyone know how do i generate a new random number so i can store them into different variables? (into temp1 and temp2) I want the program to rerun the "randomnumber" function each time for each new variable so i can store the new random value into them.
有谁知道我如何生成一个新的随机数,以便我可以将它们存储到不同的变量? (进入temp1和temp2)我希望程序每次为每个新变量重新运行“randomnumber”函数,这样我就可以将新的随机值存储到它们中。
<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function main()
{
totalscore()
leaderboard()
}
function randomnumber()
{
var randomnumber;
randomnumber = Math.random()*3;
return(Math.floor(randomnumber+0.2));
}
function totalscore()
{
var n;
var p;
var total;
var temp1;
var temp2;
var score;
score = 0;
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;
}
temp1= total
temp2= total
document.write(total)
document.write(total)
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
<INPUT NAME = "dobutton" TYPE = "button" value = "Start game" on Click = "game()">
<INPUT NAME = "dobutton" TYPE = "button" value = "Leaderboard" on Click = "leader()">
</BODY>
</HTML>
2 个解决方案
#1
I think I know what you are asking and I'll post working code below as well as an explanation of why you are getting the same random number.
我想我知道你在问什么,我会在下面发布工作代码以及为什么你得到相同的随机数的解释。
-
You have a for loop is generating the score and and returning a value that you are storing into total. The problem is you are assigning that number to both temp1 AND temp 2, so they are now the same number, then you are writing both numbers to the page. What you need to do is have 2 different calculations. the below code will fix it and give you 2 different numbers for you totals...
你有一个for循环正在生成分数,并返回一个你要存储的值。问题是你将这个数字分配给temp1和temp 2,所以它们现在是相同的数字,然后你将这两个数字写入页面。你需要做的是有2个不同的计算。以下代码将修复它并为您提供2个不同的数字...
function totalscore() { var total1 = 0; var total2 = 0; for (var n=0; n<10; n++) { total1 = total1 + randomnumber(); total2 = total2 + randomnumber(); } document.write(total1); document.write(total2); }
-
Now I was a little confused at your code because the score is compounding on itself every time it recalculates in the for loop. (ie if the first 5 scores were 1, 2, 2, 1, 3, then the total score would be 24, and not the expected 9. If that was intentional, the put in this code instead.
现在我对你的代码感到有点困惑,因为每次在for循环中重新计算时,得分都会自动复杂化。 (即如果前5个分数分别为1,2,2,1,3,那么总分将为24,而不是预期的9.如果这是故意的,则代之以此代码。
function totalscore() { var total1 = 0; var total2 = 0; var score1 = 0; var score2 = 0; for (var n=0; n<10; n++) { score1 = score1 + randomnumber(); total1 = total1 + score1; score2 = score2 + randomnumber(); total2 = total2 + score2; } document.write(total1); document.write(total2); }
Good luck with your project and learning coding!
祝你的项目和学习编码好运!
#2
Issues I am noticing
我注意到的问题
- No semicolons behind most statements
- You are returning Math.floor on randomnumber + 0.2, which makes + 0.2 do nothing.
- You are calling onclick='game()' on button click, but there is no game(), but maybe that's in code we can't see.
- You are setting both temp1 and temp2 = total, and using document.write on total twice
大多数陈述背后都没有分号
你在randomnumber + 0.2上返回Math.floor,这使得+ 0.2什么都不做。
你在点击按钮时调用onclick ='game()',但是没有游戏(),但也许在我们看不到的代码中。
您正在设置temp1和temp2 = total,并且总共使用document.write两次
Now, the question is how to generate a random number. You are doing that correctly, although you could simplify it and use Math.floor(Math.random()*3)
or return Math.floor(Math.random()*3)
. Of course, you can switch out 3 and replace it now with whatever cap you wish.
现在,问题是如何生成随机数。你正在这样做,虽然你可以简化它并使用Math.floor(Math.random()* 3)或返回Math.floor(Math.random()* 3)。当然,您可以切换3并立即用您想要的任何帽子替换它。
#1
I think I know what you are asking and I'll post working code below as well as an explanation of why you are getting the same random number.
我想我知道你在问什么,我会在下面发布工作代码以及为什么你得到相同的随机数的解释。
-
You have a for loop is generating the score and and returning a value that you are storing into total. The problem is you are assigning that number to both temp1 AND temp 2, so they are now the same number, then you are writing both numbers to the page. What you need to do is have 2 different calculations. the below code will fix it and give you 2 different numbers for you totals...
你有一个for循环正在生成分数,并返回一个你要存储的值。问题是你将这个数字分配给temp1和temp 2,所以它们现在是相同的数字,然后你将这两个数字写入页面。你需要做的是有2个不同的计算。以下代码将修复它并为您提供2个不同的数字...
function totalscore() { var total1 = 0; var total2 = 0; for (var n=0; n<10; n++) { total1 = total1 + randomnumber(); total2 = total2 + randomnumber(); } document.write(total1); document.write(total2); }
-
Now I was a little confused at your code because the score is compounding on itself every time it recalculates in the for loop. (ie if the first 5 scores were 1, 2, 2, 1, 3, then the total score would be 24, and not the expected 9. If that was intentional, the put in this code instead.
现在我对你的代码感到有点困惑,因为每次在for循环中重新计算时,得分都会自动复杂化。 (即如果前5个分数分别为1,2,2,1,3,那么总分将为24,而不是预期的9.如果这是故意的,则代之以此代码。
function totalscore() { var total1 = 0; var total2 = 0; var score1 = 0; var score2 = 0; for (var n=0; n<10; n++) { score1 = score1 + randomnumber(); total1 = total1 + score1; score2 = score2 + randomnumber(); total2 = total2 + score2; } document.write(total1); document.write(total2); }
Good luck with your project and learning coding!
祝你的项目和学习编码好运!
#2
Issues I am noticing
我注意到的问题
- No semicolons behind most statements
- You are returning Math.floor on randomnumber + 0.2, which makes + 0.2 do nothing.
- You are calling onclick='game()' on button click, but there is no game(), but maybe that's in code we can't see.
- You are setting both temp1 and temp2 = total, and using document.write on total twice
大多数陈述背后都没有分号
你在randomnumber + 0.2上返回Math.floor,这使得+ 0.2什么都不做。
你在点击按钮时调用onclick ='game()',但是没有游戏(),但也许在我们看不到的代码中。
您正在设置temp1和temp2 = total,并且总共使用document.write两次
Now, the question is how to generate a random number. You are doing that correctly, although you could simplify it and use Math.floor(Math.random()*3)
or return Math.floor(Math.random()*3)
. Of course, you can switch out 3 and replace it now with whatever cap you wish.
现在,问题是如何生成随机数。你正在这样做,虽然你可以简化它并使用Math.floor(Math.random()* 3)或返回Math.floor(Math.random()* 3)。当然,您可以切换3并立即用您想要的任何帽子替换它。