mardi 3 mars 2015

Exception in thread main error when trying to fill in a 2D array in column major form java



Code:



import java.util.*;
public class lab3 {
public static void getScores(int[][] matrix){
Scanner kb = new Scanner(System.in);
int r, c;
for (c = 0; c < matrix[0].length; c++){
for (r = 0; r < matrix.length; r++)
System.out.println("enter scores for test "+(c+1)+ " and student "+(r+1));
matrix[r][c] = kb.nextInt();
}
kb.close();
}
public static void printArray(int[][] matrix) {
for (int c = 0; c < matrix[0].length; c++)
for (int r = 0; r < matrix.length; r++){
System.out.printf("%5d", matrix[r][c]);
System.out.println();
}
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
System.out.println("how many students are there?");
int students=kb.nextInt();
System.out.println("How many tests are there?");
int tests=kb.nextInt();
int [][] grades= new int [students][tests+1];

getScores(grades);
printArray(grades);


I feel like I'm close but I keep getting an error. filling an array in column major form is the main deal so "get scores" method and the declared array at the bottom is where I think my problem is please help me




Aucun commentaire:

Enregistrer un commentaire