我试图循环访问我创建的数组的变量,以将“优胜者”拉到新数组中。 我可以把分数拿过来,但后来失去了它在阵列中的位置。拉一个变量及其在阵列中的位置
基本上,我希望它这样做:
players = [23,41,15,65,18]
Player 4 has won(with a score of 65)
我试图循环访问我创建的数组的变量,以将“优胜者”拉到新数组中。 我可以把分数拿过来,但后来失去了它在阵列中的位置。拉一个变量及其在阵列中的位置
基本上,我希望它这样做:
players = [23,41,15,65,18]
Player 4 has won(with a score of 65)
要找到一个数组的最大元素。 (以及最高得分)
int max = 0;
int player = 0;
for (int i=0; i<players.length; i++){
if (players[i] > max){
player = i+1;
max = players[i]
}
}
System.out.println("Player "+player+" has won(with a score of "+max+")");
我希望这会有所帮助。
编辑:正如有人指出的球员(如果计算第一个为1)将是我+ 1。谢谢。
我回答了您的问题吗? – lacraig2 2015-03-08 21:10:41