【发布时间】:2018-06-13 12:22:34
【问题描述】:
我正在尝试创建一个程序来计算学生的出勤率。程序计算得很好,但我的输出有问题。如果学生有 100% 的出勤率,程序将按预期显示百分比。但是当学生小于100%时,无论输入什么值,程序都会将百分比显示为0%。
图形用户界面的主要部分
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+".");
}
【问题讨论】:
-
即使没有阅读您提供的大量代码,这听起来像是一个整数除法问题,请确保您的百分比计算中提供的数字之一是浮点/小数类型
-
Too... much... code... 将事情缩小到本质,这是包括计算在内的几行代码,其中包含整数除法。简单的调试或日志记录会告诉你这一点。
-
你调试过你的代码吗?看到您发布的大部分代码都是 Netbeans 生成的 Swing 代码,我假设您使用的是包含调试器的 IDE
-
attendancePercentage=(days/maxDays)*100;和int maxDays, days;@NickA 是对的