我在一个问题上遇到了一些麻烦。我不完全理解问题是什么,但是当我尝试计算它时,我不断得到奇怪的值。它在我更改组合框值几次时起作用。但随后它开始吐出不可行的数字。错误在这里的某个地方:
private void updateTripDurationText()
{
String speed = (String)avgspeedBox.getSelectedItem();
String dist = (String)distanceText.getText();
double d = Double.parseDouble(dist);
double s = Double.parseDouble(speed);
double durationT = d/s;
String durationTime = String.format("%.1f",(Double.valueOf(durationT)));
durationText.setText(durationTime);
}
private class ComboBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setCities(destinationBox.getSelectedIndex(),originBox.getSelectedIndex());
updateDistanceText();
updateTripDurationText();
}
}
//This is the actionlistender that triggers our actions when the radio buttons are used. Miles is selected by default however.
private class RadioButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == kilometersRadioButton)
{
distanceLabel.setText("Distance (kilometers):");
avgspeedLabel.setText("Avg. Speeg (kph): ");
setKm(true);
}
else if (event.getSource() == milesRadioButton)
{
distanceLabel.setText("Distance (miles): ");
avgspeedLabel.setText("Avg. Speeg (mph): ");
setKm(false);
}
updateDistanceText();
updateTripDurationText();
}
}
public static void main(String[] args)
{
new TripCalculator();
}
}
问题完全在于 updateTripDuration。但我找不到是什么原因造成的。有关更多详细信息。当这计算两个位置之间的距离时,然后取平均速度并将距离除以它。但是,使用此设置,它只能进行一两次更改,然后它会关闭并保持关闭状态。