将信息存储在多维数组中并在JOptionPane中显示信息

时间:2012-11-21 02:10:36

标签: java arrays multidimensional-array joptionpane dimensions

我将首先列出作业的内容。然后我会列出我的问题。最后,到目前为止,我会列出我的代码,请注意,它远非完整,但在我超越这一步之前我无法继续。

ASSIGNMENT:

本编程作业的目的是为您提供使用Java多维数组,使用Button进行事件驱动编程以及使用JFrame和JOptionPane类应用AWT和Swing的实践经验。

计划要求:

使用init()编写Java Applet,用于课程分数计算,具体如下 要求:

使用JoptionPane允许教师输入学生人数,G编号(不需要学生姓名。),课程数量,课程ID(CIT230,CIT352等)以及每门课程的分数。课程分数必须存储在二维数组中。二维数组的一个例子是:

然后会显示一个表格。请记住,我们没有使用JTable。

添加按钮并使用事件驱动的编程技术,允许用户点击三个 按钮,“排序和列出学生的分数”,“显示每个学生的平均分数”和“退出”。

  1. “排序并列出学生的分数”按钮 - 显示每个学生的分类分数。
  2. “显示每位学生的平均分数” - 显示每位学生的平均分数。
  3. “退出” - 为用户创建“是”或“否”对话框以退出程序。
  4. 我的问题:我不知道如何创建这个维度基于用户输入内容的多维数组。行数是学生数,列数是课程数。当我的程序有点工作时,JOptionPane将显示[[null] [null]]。我知道如何创建一个不在JOptionPane中的二维数组,并且具有先前设置的尺寸,但这是让我离开的方法。我不是要求直接回答代码,我只需要一个示例或一些正确的方法来指导它。

    我的代码:

    (我有些事情评论说我尝试过但没有用。我尝试了很多不同的方法,但我只是删除它们。)

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Arrays;
    
    public class ScoreApplet extends JApplet implements ActionListener 
    {
        private JLabel mainMenu;
        private JButton sortScores;
        private JButton displayAverage;
        private JButton exit;
        private String[][] tableStudentScores; 
    
        private int studentNumber;
        private int courseNumber;
        private String studentNumberString;
        private String courseNumberString;
        private String scoresString;
        private double scores;
    
        public void init() 
        {
            Container contentPane = getContentPane();
            contentPane.setBackground(Color.WHITE);
    
            contentPane.setLayout(new FlowLayout());
    
            mainMenu = new JLabel("What would you like to do?");
                sortScores = new JButton("Sort And List Student's Scores");
                displayAverage = new JButton("Display Average Score for Each Student");
                exit = new JButton ("Exit");
    
            contentPane.add(mainMenu);
            contentPane.add(sortScores);
            sortScores.addActionListener(this);
            contentPane.add(displayAverage);
            displayAverage.addActionListener(this);
            contentPane.add(exit);
            exit.addActionListener(this);
    
            mainMenu.setVisible(false);
            sortScores.setVisible(false);
            displayAverage.setVisible(false);
            exit.setVisible(false);
    
            studentNumberString = JOptionPane.showInputDialog("Please enter the number of students:");
            studentNumber = Integer.parseInt(studentNumberString);
            tableStudentScores = new String[studentNumber][];
            courseNumberString = JOptionPane.showInputDialog("Please enter the number of courses:");
            courseNumber = Integer.parseInt(courseNumberString);
            tableStudentScores = new String[studentNumber][courseNumber];
    
    
            for (int index = 0; index < studentNumber; index++)
                for (index = 0; index < courseNumber; index++)
    
                    scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1));
                    scores = Double.parseDouble(scoresString);
    
    
    
    
            /*
            for (int index = 0; index <= (courseNumber - 1); index++)
            {
    
                scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1) );
                scores = Double.parseDouble(scoresString);
    
    
            }
            */
    
            JOptionPane.showMessageDialog(null, Arrays.deepToString(tableStudentScores));
    
            mainMenu.setVisible(true);
            sortScores.setVisible(true);
            displayAverage.setVisible(true);
            exit.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e)
        {
    
            if (e.getActionCommand().equals("Sort And List Student's Scores"))
            {
                /*
            }
                for (int index = 0; index < anArray.length - 1; index++)
                {   // Place the correct value in anArray[index]
                    int indexOfNextSmallest = getIndexOfSmallest(index, anArray);
                    interchange(index, indexOfNextSmallest, anArray);
                    */
            }
            else if (e.getActionCommand().equals("Display Average Score for Each Student"))
            {
    
            }
            else if (e.getActionCommand().equals("Exit"));
            {
                int answer =
                           JOptionPane.showConfirmDialog(null, "End program?",
                                         "Click Yes or No:", JOptionPane.YES_NO_OPTION);
    
                      if (answer == JOptionPane.YES_OPTION)
                          System.exit(0);
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

您应该使用括号在循环之间包含语句以将其相乘。

如果不存在某些值,则为列索引器使用另一个变量名称。

   for (int  column = 0; column < table_Columns_numbers  ; column++)

   for (int  row = 0; row < table_rows_numbers; row++)
   {

   //Statement 1   ask user to enter the value 

    JOptionPane.showInputDialog("Please enter the value"); 

   //Statement 2   assign the value to your multidimensional array 

    array[row][column] = the value entered


    }

代码中的列号是课程编号

行号是学号的编号。

因此,您使用for循环在col。的课程编号中输入学生编号索引+ 1的分数。

每次将您输入的分数编号分配给表格单元格

for (int  col = 0; col < courseNumber; col++)

      for (int  index = 0; index < studentNumber; index++)
      {
      scoresString = JOptionPane.showInputDialog("Please enter the scores for Student " + (index+1));
      scores = Double.parseDouble(scoresString);

      //This is statement for explanation  

      System.out.println("scoresString  for student no \t"+(index+1)+"\t and course number \t"+ (col+1)+"\t"+scoresString);

     //Assigning value to   tableStudentScores[index][col] cell.

     tableStudentScores[index][col]=String.valueOf( scores);


    }

    JOptionPane.showMessageDialog( null ,Arrays.deepToString(tableStudentScores));