0
这是我的停车场系统代码的一部分。我在使用'count'时遇到了一些问题。目前,我的程序为每个输入'reg no','high value'和'large vehicle'增加了1个计数。每次我只输入新的车辆登记号码时,我希望我的程序只添加1个?我该怎么做呢?用Java计算我不是很好。这是我到目前为止:计数车辆数量java
package carpark;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class textBox extends javax.swing.JFrame
implements ActionListener
{ // declare the components and variables
private final JLabel inputLabel,inputLabel2,inputLabel3,countLabel;
private final JTextField inputBox,inputBox2,inputBox3,countBox;
private final JButton enterButton;
private final JButton clearButton;
private int count;
private String sum;
private String inputString, countString;
private String sumString;
public textBox()
{ // sets up the components and places them
this.setLayout(new FlowLayout());
this.inputLabel = new JLabel("Please enter the vehicle's registration number:");
this.inputBox = new JTextField(8);
this.inputLabel2 = new JLabel("Is this a high-value car? Enter yes or no:");
this.inputBox2 = new JTextField(4);
this.inputLabel3 = new JLabel("Is this a large vehicle? Enter yes or no:");
this.inputBox3 = new JTextField(4);
this.countLabel = new JLabel(" Car Count");
this.countBox = new JTextField(3);
this.enterButton = new JButton("Enter");
this.clearButton = new JButton("Clear");
this.add(inputLabel);
this.add(inputBox);
this.add(inputLabel2);
this.add(inputBox2);
this.add(inputLabel3);
this.add(inputBox3);
this.inputBox.addActionListener(this);
this.add(countLabel);
this.add(countBox);
this.countBox.setEditable(false);
this.add(enterButton);
this.enterButton.addActionListener(this);
this.add(clearButton);
this.clearButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{ // reacts to the ENTER key being pressed
if (event.getSource() == this.enterButton || event.getSource() == this.inputBox)
try
{
this.inputString = inputBox.getText();
this.count += 1;
this.countString = Integer.toString(this.count);
this.countBox.setText(this.countString);
}
catch (NumberFormatException entry)
{
this.inputBox.setText("");
}
else if (event.getSource() == this.clearButton)
{
this.inputBox.setText("");
this.countBox.setText("");
this.count = 0;
}
}
}
你需要一个'List'或注册车辆的'Map',只增加了计数,当你看到一个新的注册号 – benji 2014-11-22 18:20:25
您不必连接到“高价值”或任何监听器“大型车辆”文本框,那么您确定在这些框中输入的内容会导致您的问题?我建议让你的'actionPerformed'方法记录事件(event.toString()'或'event.paramString()');也许计数正在增加一些你不期待的事件。虽然我有点猜测 – ajb 2014-11-22 18:26:04