简体   繁体   中英

Populating a 2D Array Java

I need to create a 2d array which can read in the student ID's of 50 students and each of their 7 subject marks. I have come up with a way to store subject marks but not sure how to store the student ID's. Here is the code so far.

public static void main(String[] args) 
{
    double mark;
    double[][] studs = new double[50][7];

    Scanner fromKeyboard = new Scanner(System.in);

    for (int studentNo = 0; studentNo < 3; studentNo++) {
        System.out.println("enter student ID number for student " + studentNo);

        for (int moduleNo = 0; moduleNo < 2; moduleNo++) {
            System.out.println("Enter users mark for module " + moduleNo);
            mark = fromKeyboard.nextDouble();
            studs[studentNo][moduleNo] = mark;
        }
    }
}

You have only one array of a single primitive type, but you have two pieces of information.

Two simple options are

1) Use another array to store the IDs

2) (Better solution IMO) Create your own Student class, and define an array Student[] (A student should contain a field for an array of marks)

You can use array[n][0] to store student id.

This should work:

public static void main(String[] args) 
{
    double mark = 0d;
    int id = 0;
    double[][] studs = new double[50][8];

    Scanner fromKeyboard = new Scanner(System.in);

    for (int studentNo = 0; studentNo < 50; studentNo++) {
        System.out.print("enter student ID number for student " + (studentNo + 1) + ":");
        id = fromKeyboard.nextInt();
        studs[studentNo][0] = id;
        for (int moduleNo = 1; moduleNo < 8; moduleNo++) {
            System.out.print("Enter mark of student " + id + " for module " + moduleNo);
            mark = fromKeyboard.nextDouble();
            studs[studentNo][moduleNo] = mark;
        }
    }

    fromKeyboard.close();
}

NOTES:

  • If you cannot modify the original array or you need to store students name, for example, you can create a new array to store students id like.

     String[] studentsId = new String[50]; int[] studentsId = new int[50]; 
  • remember to close resources when using it: fromKeyboard.close();

When I understood your question correct:

public static void main(String[] args) 
{
    double[][] studs = new double[50][8];

    Scanner fromKeyboard = new Scanner(System.in);

for (int studentNo = 0; studentNo < 50; studentNo++) {
    System.out.println("enter student ID number for student " + studentNo);
    studs[studentNo][0] = fromKeyboard.nextDouble(); //save id
    for (int moduleNo = 1; moduleNo < 8; moduleNo++) {  
        System.out.println("Enter users mark for module " + moduleNo);
        studs[studentNo][moduleNo] = fromKeyboard.nextDouble(); // your 7 marks
    }
}
}

You're on the right track.

You aren't reading the studentNo input. So you need to read that and place it in the first cell before the inner loop. Then put all the marks on the same row along side it. This is depending on the type of student ID, is it a String or number?

Also, why have 7 columns in the array and only loop twice for subject grades? Is there more to do here. If not avoid using up the space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM