在Java中接受2D数组作为输入

时间:2014-02-23 12:48:50

标签: java arrays 2d

我正在编写一个java程序,要求用户输入每个学生的标记(2个科目)(总共4个学生)。基本上,它是2D阵列操作。

我被困在需要用户输入输入的位置。我在想input.nextInt()的常规方法是行不通的。到目前为止,这是我的代码。有人可以帮忙吗?

package Chapter2_Arrays;
import java.util.*;
public class Arrays_ExTwo {

    static final int numberOfSubjects=2;
    static final int numberOfStudents=4;

    static int [][] marks=new int[numberOfSubjects][numberOfStudents];

    static Scanner in=new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Welcome to Cincinnati Elementary");
        System.out.println("There are "+ " "+ numberOfSubjects+ " "+ "subjects being taught");
        System.out.println("There are "+" "+numberOfStudents+" "+" number of students in the class");
        System.out.println("Enter the subject number followed by the marks of all the students for the subject");
        for(int i=0;i<numberOfSubjects;i++)
        {
            for(int j=0;j<numberOfStudents;j++)
            {
                marks[i][j]=in.
            }
        }

    }

} 

1 个答案:

答案 0 :(得分:0)

试试这个:

package Chapter2_Arrays;
import java.util.*;
public class Arrays_ExTwo {

    static final int numberOfSubjects=2;
    static final int numberOfStudents=4;

    // byte is a number from -128 to 127; that's big enough for 1-6 marks; you can use char for 'A'-'F' marks.
    static byte [][] marks=new byte[numberOfSubjects][numberOfStudents];

    static Scanner in=new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Welcome to Cincinnati Elementary");
        System.out.println("There are "+ " "+ numberOfSubjects+ " "+ "subjects being taught");
        System.out.println("There are "+" "+numberOfStudents+" "+" number of students in the class");

        for(int i=0;i<numberOfSubjects;i++)
        {
            System.out.println("Enter the marks of students for subject no. "+(i+1)+":");
            for(int j=0;j<numberOfStudents;j++)
            {
                marks[i][j]=in.nextByte();
                System.out.println("Mark of student no. " + (j+1) + " for subject no. " + (i+1) + "was set to " + marks[i][j]);
            }
        }

    }

}