首页 > 解决方案 > 程序的输出无法在 Java 中正确显示

问题描述

我正在尝试创建一个程序来计算学生出勤率。程序计算得很好,但我的输出有问题。如果学生有 100% 的出勤率,程序将按预期显示百分比。但是当学生小于 100% 时,无论输入什么值,程序都会将百分比显示为 0%。

GUI的主要部分

class Frame extends JFrame
{
    private JButton calcButton;
    private JTextField daysPresentText;
    private JLabel instructionLabel;
    private JComboBox<String> intakeComboBox;
    private JLabel intakeLabel;
    private JLabel maxDaysLabel;
    private JComboBox<String> semesterComboBox;
    private JLabel semesterLabel;
    private JLabel statusLabel;
    private JLabel studentLabel;
    static JTextField studentText;

    String semester, intake, daysPresent, semesterLength;
    int maxDays, days;
    static double attendancePercentage;

    public Frame() 
    {
       initComponents();


    }

     @SuppressWarnings("unchecked")                         
    private void initComponents() 
    {

        studentText = new JTextField();
        instructionLabel = new JLabel();
        statusLabel = new JLabel();
        semesterLabel = new JLabel();
        studentLabel = new JLabel();
        calcButton = new JButton();
        semesterComboBox = new JComboBox<>();
        daysPresentText = new JTextField();
        maxDaysLabel = new JLabel();
        intakeLabel = new JLabel();
        intakeComboBox = new JComboBox<>();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        instructionLabel.setText("Enter student name and days present");

        statusLabel.setText("Days Present");

        semesterLabel.setText("Semester");

        studentLabel.setText("Student");

        calcButton.setText("Calculate Percentage");
        calcButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt) 
            {
                calcButtonActionPerformed(evt);
            }
        });

        semesterComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "1", "2", "3", "4", "5","6","7"}));
        semesterComboBox.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent evt) 
            {
                semesterComboBoxItemStateChanged(evt);
            }
        });

        daysPresentText.setText("");

        intakeLabel.setText("Intake");

        intakeComboBox.setModel(new DefaultComboBoxModel<>(new String[] { "March", "August"}));
        intakeComboBox.addItemListener(new ItemListener() 
        {
            public void itemStateChanged(ItemEvent evt) 
            {
                intakeComboBoxItemStateChanged(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(studentLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(instructionLabel, GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addComponent(studentText))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(semesterComboBox, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(intakeComboBox, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(semesterLabel, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(intakeLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(daysPresentText, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maxDaysLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(statusLabel, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)))
                    .addComponent(calcButton, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instructionLabel, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(statusLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                            .addComponent(intakeLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(intakeComboBox, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                                .addComponent(daysPresentText, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
                            .addComponent(studentText)
                            .addComponent(semesterComboBox)
                            .addComponent(maxDaysLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(studentLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                        .addComponent(semesterLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(calcButton, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }     

    private void semesterComboBoxItemStateChanged(ItemEvent evt) 
    {     
     semester=evt.getItem().toString();
    }                                                 

    private void intakeComboBoxItemStateChanged(ItemEvent evt) 
    {                                                
     intake=evt.getItem().toString();
    }

    private void calcButtonActionPerformed(ActionEvent evt)
    {
       daysPresent=daysPresentText.getText();       
       days=Integer.valueOf(daysPresent);
        maxDays=98;
        if(days>maxDays)
        {
          errorMessage EM=new errorMessage();
          EM.setVisible(true);
        }else
        {
        calculate();
        CalculateDisplay CD=new CalculateDisplay();
        CD.setVisible(true);
        }
    }                      


    public void calculate()
    {
        attendancePercentage=(days/maxDays)*100;
    }
}

方程输出的显示

   class CalculateDisplay extends JFrame 
{
    private JLabel displayLabel;
    private JButton okButton;

    String student;
    double attendancePercentage;

    public CalculateDisplay() 
    {
        initComponents();
        student=Frame.studentText.getText();
        attendancePercentage=Frame.attendancePercentage;
        displayLabel.setText("The attendance percentage of "+student+" is 
        "+attendancePercentage+".");
    }

标签: javaoutput

解决方案


这是一个基本的整数除法问题整数除法:如何产生双精度数?

更改int maxDays, days;double maxDays, days;或更改您的计算方法:

attendancePercentage=(days/maxDays)*100;

至:

attendancePercentage = ((double) days / (double) maxDays) * 100;`

推荐阅读