循环遍历2D数组并分别计算每一行的结果(Java)

时间:2016-12-03 22:05:47

标签: java loops for-loop iteration

我有一个2D数组,当用户投票时存储答案是/否,所以对于我的情况,第1行是问题1,然后当问题添加到系统时它转到第2行等。选民ID是专栏,我需要的是计算是/否答案的数量,以获得每个问题的投票数。

我设法循环遍历整个数组并计算是/否答案(如果我只需要一个问题就可以工作)但是我似乎无法让我的系统打印每行的结果(问题)循环。

以下是存储答案的数组

public static List<String[][]> voteList = new ArrayList<String[][]>();
public static String[][] arrayOfAnswers = new String[100][100];
public static List<Referendum> referendumList= new ArrayList<Referendum>();

这就是我存储答案的方式

System.out.println("Enter your first name here: ");
    String first = Scanner.nextLine();

    System.out.println("Enter your surname here: ");
    String surname = Scanner.nextLine();

    System.out.println("Enter your city here: ");
    String city = Scanner.nextLine();

    User user1=new User();


    int vid=user1.getTheIndex(first, surname, city);
    System.out.println("Enter your answer for the question: ");
    String theanswer = Scanner.nextLine();



    if(theanswer.toUpperCase().equals("YES"))
    {
        arrayOfAnswers[qid][vid]=theanswer;
        voteList.add(arrayOfAnswers);
        System.out.println("Thanks for voting. \n");
    }

    else if(theanswer.toUpperCase().equals("NO"))
    {
        arrayOfAnswers[qid][vid]=theanswer;
        voteList.add(arrayOfAnswers);
        System.out.println("Thanks for voting. \n");
    }

    else
    {
        System.out.println("You must enter a yes and no answer, please try again.");

    }

这就是我试图计算每一行的是/否答案的循环(问题)

for (Referendum q: Menu.referendumList)
    {   
        int i=0;
        int yesCount = 0;
        int noCount = 0;    


        for(int j=0; j<Menu.arrayOfAnswers[i].length; j++)
        {

            if(Menu.arrayOfAnswers[i][j] != null)
                {
                    if(Menu.arrayOfAnswers[i][j].equals("yes"))
                        {
                            yesCount++;
                        }
                    else if(Menu.arrayOfAnswers[i][j].equals("no"))
                        {
                            noCount++;
                        } 
                }
        }
        System.out.print("Referendum : " + q.question + " \n");
        System.out.println("Votes yes: " + yesCount);   
        System.out.println("Votes no: " + noCount + "\n");  
    }

2 个答案:

答案 0 :(得分:0)

i循环中的本地for变量永远不会递增,您只需要计算第一行。自int i = 0起,您的if语句只会测试Menu.arrayOfAnswers[0][j]。如果您有两个问题,那么在计算完第一行后,您应该将i增加一。

答案 1 :(得分:0)

假设您有一个2D数组array2D,如果您想要处理每个元素,您必须循环遍历行,而在行内,您必须循环遍历该行的元素:

for(int i = 0; i < array2D.lenght; ++i){     // loop for the rows
    for(int j = 0; j < array2D[i].length; ++j){ //loop for the elements in each row
        // Do something with element j from row i: array2D[i][j]
    }
}

在您的情况下,数组是Menu.arrayOfAnswers。正如你想要每行计数器(问题),在循环遍历每行的元素(问题)之前,初始化循环内的行(问题)

int yesCount;
int noCount;
for(int i = 0; i < Menu.arrayOfAnswers.lenght; ++i){     // loop for the questions
    yesCount = 0;
    noCount = 0;
    for(int j = 0; j < Menu.arrayOfAnswers[i].length; ++j){ //loop for the answers in each question
        if(Menu.arrayOfAnswers[i][j] != null) {
            if(Menu.arrayOfAnswers[i][j].equals("yes")){
                yesCount++;
            }
            else if(Menu.arrayOfAnswers[i][j].equals("no")){
                noCount++;
            } 
         }
    }
}