输出相同的生成数值

时间:2023-01-05 07:39:46

Do anyone of you know how to keep a random number generator value the same when i output it? I'm currently creating a leader board where it allows user to enter their name, enter how many player are playing, and have a random number generator generate 10 value and adding them together into one total score. So far, I have generated the total score and i plan to output as "Welcome (player name) your total score is (total score value)", this process is then repeated until all player's name is outputted. After that, i plan to use an array and array sort to display the player's score from highest to lowest. But the problem is that i don't know how do i keep the "total score" value the same as the one i displayed to the one that outputs the player's name and score and the "leaderboard" since the random number generator will keep generating new number values. Some help would be greatly appreciated. Thank you. Here is what i have done so far

当你输出它时,你们中的任何人都知道如何保持随机数生成器的值相同吗?我正在创建一个排行榜,它允许用户输入他们的名字,输入正在播放的玩家数量,并让随机数生成器生成10个值并将它们一起添加到一个总分中。到目前为止,我已经产生了总得分,我计划输出“欢迎(球员姓名)你的总得分(总得分值)”,然后重复这个过程,直到输出所有球员的名字。之后,我计划使用数组和数组排序来显示玩家从最高到最低的分数。但问题是我不知道如何保持“总分”值与我显示的那个值相同,输出玩家的名字和分数以及“排行榜”,因为随机数生成器将继续生成新的数字值。一些帮助将不胜感激。谢谢。这是我到目前为止所做的

< HTML >
  < !Foundation Page
for building our Javascript programs >
  < HEAD >
  < TITLE > The Foundation Page < /TITLE>
<SCRIPT LANGUAGE = "JavaScript">

function main()
{
	start()
	randomnumber()
	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?")
		}
	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?")
		}
	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?")
		}		
}

function randomnumber()
{
	var randomnumber;
	randomnumber = Math.random()*3;
	return(Math.floor(randomnumber+0.5));
}
function totalscore() 
{
	var n;
	var score = 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;
			}
	}
	document.write (score)
}
function leaderboard ()
{
	var leaderboardarray = new array (5);

		leaderboardarray[0] = number;
		leaderboardarray[1] = number;
		leaderboardarray[2] = number;
		leaderboardarray[3] = number;
		leaderboardarray[4] = number;
	


}
</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 >

1 个解决方案

#1


Not really sure what's being asked here but just a few points to make:

不确定这里有什么问题,但只需要几点:

  • Your name variables could be made into an array of names with the size specified by ask. i.e instead of names1-5 you could do

    您的名称变量可以制成一个名称数组,其大小由ask指定。你可以做而不是名字1-5

    var names[ask]
    

    so it creates an array of names where the size is the amount of players specified. Then prompting for names you could have somethings along the lines

    所以它创建了一个名称数组,其中大小是指定的玩家数量。然后提示输入你可能有些名字的名字

    for(var i = 1; i < ask; i++) {
       names[i] = prompt("Name of Player " + i);
    }
    
  • In totalScore your if else checks for number are pointless because you're always going to be adding number to score and it will always be between 0 and 3 so instead of checking it's value to add, just add it:

    在totalScore中,if else检查数字是没有意义的,因为你总是要添加数字来得分,它总是在0到3之间,所以不要检查它的值来添加,只需添加它:

    number = randomNumber();
    score += number;
    
  • Your formatting in general is pretty poor and you should use lower case for the tags and no spaces i.e

    你的格式一般很差,你应该使用小写的标签,没有空格,即

    < HTML > 
    

    should be

    <html> 
    

    similarly with all tags

    与所有标签类似

  • Note: The above are tips that I think you should take on board for future work!

    注意:以上是我认为您应该留在以后工作的提示!

  • If you could clarify what exactly you want to do with the above code that would be great and I'd be glad to try help!
  • 如果你能澄清一下你想用上面的代码做什么,这将是很棒的,我很乐意尝试帮助!

#1


Not really sure what's being asked here but just a few points to make:

不确定这里有什么问题,但只需要几点:

  • Your name variables could be made into an array of names with the size specified by ask. i.e instead of names1-5 you could do

    您的名称变量可以制成一个名称数组,其大小由ask指定。你可以做而不是名字1-5

    var names[ask]
    

    so it creates an array of names where the size is the amount of players specified. Then prompting for names you could have somethings along the lines

    所以它创建了一个名称数组,其中大小是指定的玩家数量。然后提示输入你可能有些名字的名字

    for(var i = 1; i < ask; i++) {
       names[i] = prompt("Name of Player " + i);
    }
    
  • In totalScore your if else checks for number are pointless because you're always going to be adding number to score and it will always be between 0 and 3 so instead of checking it's value to add, just add it:

    在totalScore中,if else检查数字是没有意义的,因为你总是要添加数字来得分,它总是在0到3之间,所以不要检查它的值来添加,只需添加它:

    number = randomNumber();
    score += number;
    
  • Your formatting in general is pretty poor and you should use lower case for the tags and no spaces i.e

    你的格式一般很差,你应该使用小写的标签,没有空格,即

    < HTML > 
    

    should be

    <html> 
    

    similarly with all tags

    与所有标签类似

  • Note: The above are tips that I think you should take on board for future work!

    注意:以上是我认为您应该留在以后工作的提示!

  • If you could clarify what exactly you want to do with the above code that would be great and I'd be glad to try help!
  • 如果你能澄清一下你想用上面的代码做什么,这将是很棒的,我很乐意尝试帮助!