2d数组使用if语句检查rows值

问题描述 投票:1回答:2

所以我试图循环遍历2d数组的行来检查该行是否与方法的属性匹配。如何使用if来检查行?这是我的代码

 public void recordWhiplashPoints(ConnectionToClient client, int vote){


    int[][] votecount = new int[game.getPlayers().length][0];


    outside:
    if(game.getRecordedAnswers() <= game.getPlayers().length){
    for (int i = 0; i < game.getPlayers().length; i++) {
        for (int q = 0; q < votecount.length; q++) {
            if(votecount[q] == vote){
                //do stuff
            }

        }
      } 
    }
}

因此,投票计数[行]是。我可以用某种方式与房产投票进行比较吗?

java arrays
2个回答
1
投票

因此,对于二维数组(基本上只是一个数组数组),您可以使用类似votecount[i]的成员数组,并使用votecount[i][q]获得该数组的成员。我认为以下是您想要的代码:

int[][] votecount = new int[game.getPlayers().length][0];

outside:
if(game.getRecordedAnswers() <= game.getPlayers().length){
for (int i = 0; i < length; i++) {
    // note that we need to compare against the array votecount[i]
    for (int q = 0; q < votecount[i].length; q++) {
        // here we access the actual element votecount[i][q]
        if(votecount[i][q] == vote){
            //do stuff
        }
    }
  } 
}

0
投票

不确定这是否是您正在寻找的,但一种方法是使用for-each循环

public void recordWhiplashPoints(ConnectionToClient client, int vote){


int[][] votecount = new int[game.getPlayers().length][0];


outside:
if(game.getRecordedAnswers() <= game.getPlayers().length){
for (int[] i : votecount) {
    for (int q : i) {
        if(q == vote){
            //do stuff
        }

    }
  } 
}

}

本质上,第一个for-each循环遍历每个数组的2d votecount数组,然后第二个for-each循环遍历每个1D数组。如果你有问题,就问吧。

但是,我不明白你的第二个if语句是如何真实的,因为你永远不会改变其他任何默认值的投票数,这是一个填充0的二维数组。

© www.soinside.com 2019 - 2024. All rights reserved.