繁体   English   中英

如何并排打印两个2D阵列?

[英]How do I print two 2D arrays side by side?

因此,用户在不同的字段中输入他/她的名字和姓氏。 (必须那样)

参考图片: 在此处输入图片说明

我要尝试的是在其4个测试标记旁边显示其名称。

所以看起来像这样

John Smith 55.0 100.0 23.050。Jane Smith 100.0 50.0 76.0 22.0,依此类推。

我的代码是

public class StudentGradesView extends FrameView {
    final static int students=15;
    final static int numOfTest=4;
    final static int firstNLast=2;

    //Start with the first student in the 2D array
    private int row = 0;


    //DELCARING THE 2D ARRAY
    double[][] marks = new double[students][numOfTest];
    String[][] names = new String[students][firstNLast];

    //1D Arrays
    double testScores[]=new double[4];
    String studentNames[]=new String[2];


    public void addInfo(){
        studentNames[0]= firstNameIn.getText();
        studentNames[1]= lastNameIn.getText();

        testScores[0]=Double.parseDouble(testOneIn.getText());
        testScores[1]=Double.parseDouble(testTwoIn.getText());
        testScores[2]=Double.parseDouble(testThreeIn.getText());
        testScores[3]=Double.parseDouble(testFourIn.getText());

        //Add first and last name to 2d array
        for(int col=0;col <studentNames.length; col++){
            names[row][col]= studentNames[col];
        }

        //Add all four test scores to the current student
        for(int col=0;col < testScores.length; col++){
            marks[row][col]= testScores[col];
        }
        //Advance to the next student
        row++;
    }

    public void displayArray(){
        for(int i=0;i<names.length;i++){
            for(int j=0; j<firstNLast;j++){
                finalOutput.append(names[i][j]+" ");
            }
            finalOutput.append("\n");
        }

        for(int i=0; i<marks.length;i++){
            for(int j=0; j <numOfTest; j++){
                finalOutput.append(marks[i][j]+" ");
            }
            finalOutput.append("\n");
        }
    }
}

当我点击list / displayArray()时,最终得到的是什么? 这是: 与数字分开显示名称。

像这样

因此,我坚持如何使它们在彼此旁边打印。 还有没有办法只打印用户输入的条目数? 例如,如果一个用户输入了一组信息,则仅打印一行?

谢谢。

在您的displayArray方法中,我将首先通过调用finalOutput.setText("")清除JTextArea,然后在for循环中从0转到行-到目前为止已添加的学生数。 否则,您将遍历整个数组,该数组将在行索引上方具有所有null。

更好-不使用Arrays,而是使用ArrayLists。

更好的是:创建一个保存单个学生的姓名和成绩的Student类,并使用一个ArrayList<Student>

甚至更好-在JTable中显示上面的数据。

因为这显然是一项学习练习,所以我不会为您提供代码。

快速而肮脏的答案是将显示代码更改为如下所示:

public void displayArray(){
    for (int i = 0; i < names.length; i++) {
        if (/* some test to check that the entry is populated */) {
            // append the names for student i
            // append the scores for student i 
            // append newline 
        }
    }
}

我将留给您填写详细信息。


但是这里真正的问题是您犯了许多设计错误。

  1. 将名称和分数存储为数组是一个坏主意。 Java提供了更易于使用并且没有固定长度的List类型。

    • 您不必为每个学生设置固定的分数。

    • 您不需要每个学生都有固定数量的姓名。

    • 您不需要有固定数量的学生。

  2. 您应该创建一个类来代表每个学生。 这可能真的很简单,仅包含名称和得分列表的获取器和设置器,或者您可以尝试封装状态...

  3. 为Student创建一个类之后,可以使用JList (或JTable )显示List<Student> 显示屏看起来会更好,并且您将不需要“列表”按钮。

(现在,您的讲师可能会解决这些问题……并且在此之后的练习涉及实现这些事情……)

如果数组是并行的,则可以使用一个循环:

public void displayArray()
{
    for(int i = 0 ; i < names.length ; i++)
    {
        /*
         *  If the first or last name are null then skip over and go to the 
         *      next item
         */
        if(names[i][0] == null || names[i][1] == null)
        {
            continue;
        }

        for(int j = 0; j < firstNLast; j++)
        {
            finalOutput.append(
                String.format("%10s%s%10s%s", 
                        names[i][j], " ", marks[i][j], " "));
        }

        finalOutput.append("\n");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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