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

标签 java arrays multidimensional-array joptionpane dimensions

我会先列出任务是什么。然后我会列出我的问题。最后,请注意,我将列出目前为止的代码,它远未完成,但在完成这一步之前我无法继续。

作业:

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

计划要求:

使用 init() 编写一个 Java Applet,用于计算类(class)分数,如下所示 要求:

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

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

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

  1. “排序并列出学生的分数”按钮 -- 显示每个学生的排序分数。
  2. “显示每个学生的平均分数”-- 显示每个学生的平均分数。
  3. “退出”——创建一个是或否对话框供用户退出程序。

我的问题:我不知道如何创建这个维度基于用户输入内容的多维数组。行是学生数,列是类(class)数。当我的程序开始运行时,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);
        }
    }

}

最佳答案

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

其次,如果您不这样做,则为列索引器使用另一个变量名称,一些值将为空。

   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


    }

你代码中的列号是类(class)号的编号

行号是学号的编号。

所以你用for循环将学号index+1的分数输入到col的课号中。

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

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

关于java - 在多维数组中存储信息并在JOptionPane中显示信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13485335/

相关文章:

java - 使用所属类的lazy-init选项运行计划方法

JavaFX 自定义对话框未居中

ruby - 如何从数组元素中删除一个字符?

ios - 我无法在 swift 3 中将数据从模型类传递到 TableView ?

java - 二维数组旋转

java - jOOQ:允许的字符约束?

java - native ios 应用程序中的 session 操作

Android:从代码更新 arrays.xml

java - 维度 2 的自定义对象数组的初始化

javascript - 如何转换经典 .ASP VB 多维数组(来自记录集)以便在 javascript .js 文件中使用?