java,从多个数组中获取最高值

时间:2014-02-25 20:01:48

标签: java arrays comparison

int[] player1,player2,player3,player4;
int[] player1 =new int[12];
int[] player2 =new int[12];
int[] player3 =new int[12];
int[] player4 =new int[12];

每个玩家里面都有13个随机数。 有没有办法比较player1 [i]与players2,3,4 [i]找到最高值? 价值最高的玩家将赢得那一轮。

int score = 0;

for (int i = 0; i < player1[i]; i++){
    if (player[i] > score)
    score = player[i];
}

这是我制作的代码,但它只能比较一个得分的玩家。

1 个答案:

答案 0 :(得分:1)

我会像这样写

public static int max(int... nums) {
    int max = Integer.MIN_VALUE;
    for(int i: nums) if(max < i) max = i;
    return max;
}

int[] player1,player2,player3,player4;
....
int allMax = max(max(player1), max(player2), max(player3), max(player4));

即。最大值,是各个最大值的最大值。

相关问题