因此,在您按下Reset(重置)按钮或出现错误消息后,所有值都变为空白,但是当您继续使用该程序但应该重新开始时,这些值将继续添加。
public GroceryCalc() {
initComponents();
purchase = 0;
numitems = 0;
}
public void recordPurchase(double item_price) {
purchase = purchase + item_price;
numitems++;
}
public double getPurchase() {
return purchase;
}
public int getItems() {
return numitems;
}
private void
Checkout_ButtonActionPerformed(java.awt.event.ActionEvent
evt) {
double item_price;
String purchase_string;
String num_items_string;
String item_price_string = "";
NumberFormat n = NumberFormat.getCurrencyInstance();
boolean keep_purchasing = true;
while (keep_purchasing) {
try {
item_price_string = JOptionPane.showInputDialog(null,
"Enter Item Price", "Enter Price", JOptionPane.PLAIN_MESSAGE);
if ((item_price_string != null) &&
(item_price_string.length() > 0)) {
item_price = Double.parseDouble(item_price_string);
recordPurchase(item_price);
purchase_string = n.format(purchase);
num_items_string = Integer.toString(numitems);
Item_Price_Text.setText(n.format(item_price));
Sub_Total_Text.setText(purchase_string);
Num_Items_Text.setText(num_items_string);
} else {
keep_purchasing = false;
Sales_Tax_Text.setText(n.format(purchase * 0.065));
Total_Sale_Text.setText(n.format(purchase + purchase
* 0.065));
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Your input must be
numeric!", "Bad Data!", JOptionPane.ERROR_MESSAGE);
Item_Price_Text.setText("");
Sub_Total_Text.setText("");
Num_Items_Text.setText("");
Sales_Tax_Text.setText("");
Total_Sale_Text.setText("");
if (item_price_string.isEmpty()) {
return;
}
}
}
我希望点击重置按钮或从错误消息中单击“确定”后,所有值都将完全重置
答案 0 :(得分:0)
只需将行purchase = 0;
添加到您的catch
语句中就可以了。您可能希望所有人都在numitems = 0;
语句中添加catch
。