【问题标题】:How do I print two 2D arrays side by side?如何并排打印两个二维数组?
【发布时间】:2014-05-03 23:40:38
【问题描述】:

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

参考图片:

我要做的是在他们的 4 个测试分数旁边显示他们的名字。

所以它看起来像这样

约翰·史密斯 55.0 100.0 23.0 50。 简·史密斯 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(); 时我最终得到了什么这是:

所以我被困在如何让它们并排打印。还有一种方法可以只打印用户输入的条目数吗?如果一个用户输入了一组信息,只打印一行?

谢谢。

【问题讨论】:

  • 请修正您的代码格式,因为它很难阅读,难以理解和帮助。
  • 好的。我对此很抱歉。 :)
  • 只需将所有制表符替换为空格即可。一般来说,最好不要在代码中使用制表符。
  • 所有这些文本修改都是红鲱鱼;使用 JTable 并完成它!

标签: java arrays swing user-interface


【解决方案1】:

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

更好——不要使用数组,而是使用 ArrayList。

更好的是:创建一个包含单个学生姓名和成绩的 Student 类,并使用单个 ArrayList&lt;Student&gt;

更好的是——在 JTable 中显示上面的数据。

【讨论】:

  • 不幸的是,作业要求使用二维数组,我还没有学到很多关于类的知识。另外,我将如何使用 arraylist 来存储这两种类型的数据?这不会引起问题吗?谢谢
【解决方案2】:

因为这显然是一个学习练习,所以我不会给你代码。

快速而肮脏的答案是将显示代码更改为以下内容:

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. 您应该创建一个类来代表每个学生。它可能非常简单,仅由名称和分数列表的 getter 和 setter 组成,或者您可以尝试封装状态...

  3. 为学生创建班级后,您可以使用JList(或JTable)显示List&lt;Student&gt;。显示效果会更好,您不需要“列表”按钮。

(现在有可能,这些事情将由你的讲师来解决......并且在这之后的练习涉及实施这些事情......)

【讨论】:

    【解决方案3】:

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

    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");
        }
    }
    

    【讨论】:

    • 试试我添加的编辑,它将用 10 个空格填充 namesmarks 输出
    • 它仍然不正确。它应该像这样 FirstName LastName Test1 Test2 Test3 Test4 而是覆盖两个分数并显示如下 FirstName Test1 LastName Test2 谢谢您的时间!
    • 在 for 循环中试试这个:String.format("names=%s marks=%s", names[i][j], marks[i][j]); 并粘贴结果。我需要看看数据如何存储在数组中以帮助您。
    • 我最终得到了它,但感谢您的出色帮助。也感谢其他所有人!
    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2022-11-17
    • 2016-01-25
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多