如何在Java中将数组打印为表

时间:2017-10-31 12:31:19

标签: java arrays tabular

我有两个数组,一个是String,另一个是int:

创建数组:

// Data //
     /* Marks array*/            static int[] marks = new int[3];
     /* Subject name array*/   static String[] subjects = new String[3];
// Data //

让用户在每个数组中输入3个值:

    for (int i = 0; i < subjects.length; i++)
    {
        System.out.println("Input the names of your subjects");
        subjects[i] = scan.next();
        System.out.println("");
    }                
    for (int i = 0; i < marks.length; i++)
    {
        System.out.println("Input your marks");
        marks[i] = scan.nextInt();
        System.out.println("");
    }

输出数组:

    System.out.println(Arrays.toString(subjects));
    System.out.println(Arrays.toString(marks));

代码可以工作,除了输出之外的所有东西都是这样的:

[Maths, History, French]
[10, 8, 6]

我想让它显示为一个独立于值的长度的表:

[Maths, History, French]
[   10,       8,      6]

0 个答案:

没有答案