Q:

How to store results of for loop as an array in java? Is there any other way I can write this code neat way?

'''public class question6a {
public static void main(String[] args) {

'''

This is to store the answers given by students

    char[][] answers = { { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
            { 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' },
            { 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' },
            { 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' },
            { 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
            { 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
            { 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
            { 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, };

This is the key to check answers, wheather they are correct or not.

    char[] key = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' };

This is to check answers by comparing the values of two arrays.

    for (int i = 0; i < answers.length; i++) {
        for (int j = 0; j <= 9; j++) {
            if (answers[i][j] == key[j])  // comparing elements(Chars) of two arrays.

This code to add the no. of correct answers given by students.

                {int sum = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0;
                switch (i) {
                case 0:
                    sum++;
                    break;
                case 1:
                    sum1++;
                    break;
                case 2:
                    sum2++;
                    break;
                case 3:
                    sum3++;
                    break;
                case 4:
                    sum4++;
                    break;
                case 5:
                    sum5++;
                    break;
                case 6:
                    sum6++;
                    break;
                case 7:
                    sum7++;
                    break;

                }
               }

'''
        }

    }

'''

This is to print the result of students.

    System.out.println("Student 0 got " + sum + " Marks. ");
    System.out.println("Student 1 got " + sum1 + " Marks. ");
    System.out.println("Student 2 got " + sum2 + " Marks. ");
    System.out.println("Student 3 got " + sum3 + " Marks. ");
    System.out.println("Student 4 got " + sum4 + " Marks. ");
    System.out.println("Student 5 got " + sum5 + " Marks. ");
    System.out.println("Student 6 got " + sum6 + " Marks. ");
    System.out.println("Student 7 got " + sum7 + " Marks. ");

}

}

A:

You could create an int array to save all the marks for students
public static void main(String[] args) {
     char[][] answers = { 
          { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
          { 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' },
          { 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' },
          { 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' },
          { 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
          { 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
          { 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' },
          { 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }
    };

    char[] key = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' };

    int[] studentsMarks = new int[answers.length];

    for (int i = 0; i < answers.length; i++) {
      for (int j = 0; j < answers[i].length; j++) {
        if (answers[i][j] == key[j]) {
          studentsMarks[i] += 1;
        }
      }

      System.out.println("Student "+ (i + 1) + " got " + studentsMarks[i] + " Marks.");
    }
}

output:
Student 1 got 7 Marks.
Student 2 got 6 Marks.
Student 3 got 5 Marks.
Student 4 got 4 Marks.
Student 5 got 8 Marks.
Student 6 got 7 Marks.
Student 7 got 7 Marks.
Student 8 got 7 Marks.

