Java JDK, Java Swing - 从 double 到 int 的可能有损转换

问题描述 投票:0回答:0
public HomePage() {
    aList = new ArrayList<>(20);
    String[] names = {"Nguyen Xuan Trung", "Nguyen Van Du", "Nguyen Phuc Hung", "Bui Hai Nam", "Nguyen Ba Thanh",};
    Random rd = new Random();
    for (int i = 0; i < 20; i++) {
        double regularExam = rd.nextDouble() * 10;
        double midTermExam = rd.nextDouble() * 10;
        double finalExam = rd.nextDouble() * 10;
        aList.add(new Student(names[rd.nextInt(names.length)], regularExam[rd.(nextDouble()]));
//            aList.add(new Student(i + 1, name, regularExam, midTermExam, finalExam));
    }
    initComponents();
    tbStudent.setDefaultEditor(Object.class, null);

    String[] titles = {"ID", "Student Name", "Regular Exam", "Mid-Term Exam", "Final Exam"};
    dfTableModel = new DefaultTableModel(titles, 0);
    for (Student each : aList) {
        Object[] row = {each.getStudentID(), each.getStudentName(),
            String.format("%.2f", each.getRegularExam()),
            String.format("%.2f", each.getMidTermExam()),
            String.format("%.2f", each.getFinalExam())};
        dfTableModel.addRow(row);
    }
    tbStudent.setModel(dfTableModel);
    lbNumberStudent.setText(dfTableModel.getRowCount() + "");

    //Sort
    sorter = new TableRowSorter<DefaultTableModel>(dfTableModel);
    tbStudent.setRowSorter(sorter);

    ///
    btnSubmit.setVisible(false);
    btnCancel.setVisible(false);
}

这是我的学生班级:

public class Student {

    private int studentID;
    private String studentName;
    private double regularExam;
    private double midTermExam;
    private double finalExam;
    private static int COUNT = 101;

    // Constructor
    public Student(int studentID, String studentName, double regularExam, double midTermExam, double finalExam) {
        this.studentID = COUNT;
        this.studentName = studentName;
        this.regularExam = regularExam;
        this.midTermExam = midTermExam;
        this.finalExam = finalExam;
        COUNT += 101;
    }

    /**
     * @return the studentID
     */
    public int getStudentID() {
        return studentID;
    }

    /**
     * @return the studentName
     */
    public String getStudentName() {
        return studentName;
    }

    /**
     * @param studentName the studentName to set
     */
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    /**
     * @return the regularExam
     */
    public double getRegularExam() {
        return regularExam;
    }

    /**
     * @param regularExam the regularExam to set
     */
    public void setRegularExam(double regularExam) {
        this.regularExam = regularExam;
    }

    /**
     * @return the midTermExam
     */
    public double getMidTermExam() {
        return midTermExam;
    }

    /**
     * @param midTermExam the midTermExam to set
     */
    public void setMidTermExam(double midTermExam) {
        this.midTermExam = midTermExam;
    }

    /**
     * @return the finalExam
     */
    public double getFinalExam() {
        return finalExam;
    }

    /**
     * @param finalExam the finalExam to set
     */
    public void setFinalExam(double finalExam) {
        this.finalExam = finalExam;
    }

    /**
     * @return the COUNT
     */
    public static int getCOUNT() {
        return COUNT;
    }
}

我在尝试使用另一种方法*“不兼容类型:从 double 到 int 的可能有损转换”来添加新学生时遇到问题,无法将新学生添加到 aList。我更新了学生班级,因为有人说问题可能出在我的学生班级。有人可以再次帮助我

java arraylist integer double
© www.soinside.com 2019 - 2024. All rights reserved.