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

Java 在多维数组中存储信息并在JOptionPane中显示信息,java,arrays,multidimensional-array,joptionpane,dimensions,Java,Arrays,Multidimensional Array,Joptionpane,Dimensions,我将首先列出作业内容。然后我会列出我的问题。最后,我会列出我的代码到目前为止,提醒你,它远未完成,但我不能继续,直到我通过这一步 任务: 本编程作业的目的是让您亲身体验使用Java多维数组、使用Button进行事件驱动编程以及使用JFrame和JOptionPane类应用AWT和Swing 课程要求: 使用init()编写一个Java小程序,用于计算课程分数,方法如下 要求: 使用JoptionPane允许讲师输入学生人数、G编号(不需要学生姓名)、课程编号、课程ID(CIT330、CIT352

我将首先列出作业内容。然后我会列出我的问题。最后,我会列出我的代码到目前为止,提醒你,它远未完成,但我不能继续,直到我通过这一步

任务:

本编程作业的目的是让您亲身体验使用Java多维数组、使用Button进行事件驱动编程以及使用JFrame和JOptionPane类应用AWT和Swing

课程要求:

使用init()编写一个Java小程序,用于计算课程分数,方法如下 要求:

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

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

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

  • “排序并列出学生分数”按钮——显示每个学生的排序分数
  • “显示每个学生的平均分数”--显示每个学生的平均分数
  • “退出”--创建一个“是”或“否”对话框,供用户退出程序
  • 我的问题是:我不知道如何创建这个多维数组,谁的维度是基于用户输入的。行表示学生数量,列表示课程数量。当我的程序开始工作时,JOptionPane将显示[[null][null]]。我知道如何创建一个二维数组,而不是在JOptionPane中,并且使用以前设置的维度,但这让我很扫兴。我不是要直接回答代码,我只是需要一个例子或一些正确的写作方法的指导

    我的代码:

    (我对一些我尝试过但没有成功的东西进行了评论。我尝试了很多不同的方法,但我只是删除了它们。)

    import javax.swing.*;
    导入java.awt.*;
    导入java.awt.event.*;
    导入java.util.array;
    公共类ScoreApplet扩展JApplet实现ActionListener
    {
    专用JLabel主菜单;
    私人JButton sortScores;
    私有按钮显示平均值;
    私人按钮出口;
    私有字符串[][]表学生分数;
    私人int学生号;
    私家车;
    私有字符串studentNumberString;
    私有字符串;枚举字符串;
    私有字符串scoresString;
    私人双积分;
    公共void init()
    {
    容器contentPane=getContentPane();
    contentPane.setBackground(颜色:白色);
    setLayout(新的FlowLayout());
    mainMenu=new JLabel(“您想做什么?”);
    sortScores=newjbutton(“排序并列出学生分数”);
    displayAverage=新的JButton(“显示每个学生的平均分数”);
    退出=新的JButton(“退出”);
    contentPane.add(主菜单);
    contentPane.add(sortScores);
    sortScores.addActionListener(this);
    contentPane.add(displayAverage);
    displayAverage.addActionListener(此);
    contentPane.add(退出);
    exit.addActionListener(this);
    main menu.setVisible(假);
    sortScores.setVisible(false);
    displayAverage.setVisible(假);
    exit.setVisible(false);
    studentNumberString=JOptionPane.showInputDialog(“请输入学生人数:”);
    studentNumber=Integer.parseInt(studentNumberString);
    tableStudentScores=新字符串[studentNumber][];
    courseNumberString=JOptionPane.showInputDialog(“请输入课程数:”);
    courseNumber=Integer.parseInt(courseNumberString);
    tableStudentScores=新字符串[studentNumber][courseNumber];
    对于(int index=0;index对于(int index=0;index,应该使用括号将语句包含在循环之间,以使其相乘

    其次,为列索引器使用另一个变量名,如果您不知道,某些值将为null

       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(int column=0;column
    代码中的列编号是课程编号

    行编号是学生编号的编号

    因此,您可以使用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));
    
    for(int col=0;col
    “程序要求:编写Java小程序…”我应该打他一顿
    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));