Java 数组从下一个遍历到最大值
我正在做一个有N个玩家的简单游戏。假设N=5,则玩家将: 玩家1,玩家2,玩家3,玩家4,玩家5 和比赛一样,五分之一的人将获胜。这就是获胜的逻辑。 每个玩家可以从以下数组中获得任意一个值,即数组中的随机值 0,1,2,4,8,16 得分最高的选手将获胜 对于示例案例,我已经生成了0到分数数组(6)大小之间的随机索引,并分配给每个玩家Java 数组从下一个遍历到最大值,java,arrays,Java,Arrays,我正在做一个有N个玩家的简单游戏。假设N=5,则玩家将: 玩家1,玩家2,玩家3,玩家4,玩家5 和比赛一样,五分之一的人将获胜。这就是获胜的逻辑。 每个玩家可以从以下数组中获得任意一个值,即数组中的随机值 0,1,2,4,8,16 得分最高的选手将获胜 对于示例案例,我已经生成了0到分数数组(6)大小之间的随机索引,并分配给每个玩家 int[] data = {0,1,2,4,8,16}; int[] samples = new int[5]; Random rand
int[] data = {0,1,2,4,8,16};
int[] samples = new int[5];
Random random = new Random();
for(int j=0;j<5;j++){
int value = random.nextInt(6);
samples[j] = data[value];
}
上述案例适用于第二轮。如何做到这一点?
如何像这样迭代数组,以便在10轮中找到赢家
如果您有任何建议和帮助,我们将不胜感激。只要这只是练习,而不是家庭作业,以下就是有效的方法:
int[] data = {0,1,2,4,8,16};
int[] samples = new int[5];
Random random = new Random();
int value;
int round = 3; // take in the number of rounds.
int[] wins = {0,0,0,0,0}; // we'll use this to store the player # of wins.
// Run the game for the specified number of rounds.
for(int i = 0; i < round; i++){
// Get 5 random numbers for each player.
for(int j=0;j<5;j++){
value = random.nextInt(6);
samples[j] = data[value];
}
// Set the current winner to junk values.
int max = Integer.MIN_VALUE;
int winner = 0;
// Run though the samples for the current round.
for(int j = 0; j < samples.length; j++){
// Print test of each players number.
System.out.println("Player" + (j + 1) + " Score " + samples[j]);
// Check in order which player won the round.
if(samples[j] > max){
max = samples[j];
winner = j;
}
}
// Increment the number of wins for the winner.
wins[winner]++;
System.out.println();
}
// Print test of round wins.
for(int i = 0; i < wins.length; i++){
System.out.println("Player" + (i+1) + " Wins " + wins[i]);
}
}
int[]data={0,1,2,4,8,16};
int[]样本=新int[5];
随机=新随机();
int值;
整数舍入=3;//接受回合数。
int[]wins={0,0,0,0};//我们将使用它来存储玩家的胜利。
//按指定的回合数运行游戏。
for(int i=0;i
只要这只是练习,而不是家庭作业,以下就是有效的方法:
int[] data = {0,1,2,4,8,16};
int[] samples = new int[5];
Random random = new Random();
int value;
int round = 3; // take in the number of rounds.
int[] wins = {0,0,0,0,0}; // we'll use this to store the player # of wins.
// Run the game for the specified number of rounds.
for(int i = 0; i < round; i++){
// Get 5 random numbers for each player.
for(int j=0;j<5;j++){
value = random.nextInt(6);
samples[j] = data[value];
}
// Set the current winner to junk values.
int max = Integer.MIN_VALUE;
int winner = 0;
// Run though the samples for the current round.
for(int j = 0; j < samples.length; j++){
// Print test of each players number.
System.out.println("Player" + (j + 1) + " Score " + samples[j]);
// Check in order which player won the round.
if(samples[j] > max){
max = samples[j];
winner = j;
}
}
// Increment the number of wins for the winner.
wins[winner]++;
System.out.println();
}
// Print test of round wins.
for(int i = 0; i < wins.length; i++){
System.out.println("Player" + (i+1) + " Wins " + wins[i]);
}
}
int[]data={0,1,2,4,8,16};
int[]样本=新int[5];
随机=新随机();
int值;
整数舍入=3;//接受回合数。
int[]wins={0,0,0,0};//我们将使用它来存储玩家的胜利。
//按指定的回合数运行游戏。
for(int i=0;i
也许这会有所帮助。要从某个索引开始迭代示例的所有元素开始
:
for (int j = 0; j < samples.length; j++) {
int si = (start + j) % samples.length;
System.out.println("Loop count: " + j + " looking at samples[" + si + "]");
}
for(int j=0;j
也许这会有所帮助。要从某个索引开始迭代示例的所有元素开始
:
for (int j = 0; j < samples.length; j++) {
int si = (start + j) % samples.length;
System.out.println("Loop count: " + j + " looking at samples[" + si + "]");
}
for(int j=0;j
player是一个定义了参数的对象吗?这里,只需一个大小为5的简单数组。索引define player和值define scoreplayer是一个具有已定义参数的对象吗?这里,只是大小为5的简单数组。索引定义球员和值定义分数我只是想找出问题背后的逻辑,我在这里找到了答案。谢谢,我只是想找出问题背后的逻辑,我在这里找到了答案。谢谢