【发布时间】:2013-12-13 04:07:10
【问题描述】:
我必须在我的程序中使用这两种方法,但我不知道它们的作用,因为我的程序在没有这些的情况下按照我想要的方式工作,并且当我将它们放入我的代码中时,它不会对输出或任何东西。
public double getPurchase() {
return purchase;
}
public int getItems() {
return numItems;
}
这是我的其余代码:
public class GroceryListIU extends javax.swing.JFrame {
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
public double itemPrice;
public final double SALES_TAX = 0.065;
public double totalPrice;
public double tax;
public double purchase;
public int numItems;
/**
* Creates new form GroceryListIU
*/
public GroceryListIU() {
initComponents();
//delcares purchase and numItems and resets them to 0
purchase = 0;
numItems = 0;
}
//set method to add item price
public void recordPurchase(double itemPrice) {
purchase = purchase + itemPrice;
numItems++;
}
public double getPurchase() {
return purchase;
}
public int getItems() {
return numItems;
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
//clicking exit ends the program
System.exit(0);
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
//When the user clicks "reset" all variables are set to blank or 0
txtItemPrice.setText("");
txtSubTotal.setText("");
txtNumberOfItems.setText("");
txtSalesTax.setText("");
txtTotalPrice.setText("");
numItems = 0;
purchase = 0;
}
private void btnCheckoutActionPerformed(java.awt.event.ActionEvent evt) {
boolean keepShopping = true;
JFrame frame = new JFrame();
while (keepShopping) {
try {
//When the user clicks "checkout" a input dialog will appear to enter the item price
String newItemPrice = JOptionPane.showInputDialog(frame,
"Enter Item Price",
"Enter Price",
JOptionPane.PLAIN_MESSAGE);
//if the user clicks cancel or clicks OK and left the text field blank, calculations will be made
if ((newItemPrice != null) && (newItemPrice.length() > 0)) {
//parse the double item price
itemPrice = Double.parseDouble(newItemPrice);
//takes itemPrice and plugs it into recordPurchase method
recordPurchase(itemPrice);
//adds 1 to txtNumberOfItems each time the user enters a number until it ends
txtNumberOfItems.setText((numItems) + "");
//adds item price to the txtItemPrice text field
txtItemPrice.setText(defaultFormat.format(itemPrice));
//adds the sub total to the txtSubTotal text field
txtSubTotal.setText(defaultFormat.format(purchase));
} else {
//when the user clicks ok when blank or cancel the program does the rest of the calculations
keepShopping = false;
//tax is 6.5%, you would multiply that by the purchase total
tax = SALES_TAX * purchase;
//sets "tax" in the txtSalesTax text field
txtSalesTax.setText(defaultFormat.format(tax));
//the total price is tax plus the sub total
totalPrice = tax + purchase;
//add the total price to the totalPrice text field
txtTotalPrice.setText(defaultFormat.format(totalPrice));
}
} catch (NumberFormatException e) { //if the user enters string data, an error will appear
JOptionPane.showMessageDialog(frame,
"You must enter positive numerical data!",
"Bad Data!",
JOptionPane.ERROR_MESSAGE);
}
}
}
如何在我的程序中使用它们?
【问题讨论】:
-
假设我在桌子上放了一个盘子,然后我从没想过或使用它。有问题吗?
-
我在问如何在我的程序中使用它们
-
我完全不喜欢这种问题:这家伙来了,根本没有搜索,似乎完全没有编程技能,问了一个愚蠢的问题,没关系。有人回答,得到 5 个赞,每个人都很高兴。最后,这对其他任何人都不会有用。谢谢你的OP。
-
@OlivierH 你的问题对很多人来说总是有趣/有用吗?
-
虽然问题很弱,但@MarounMaroun 的回答很好。投票是为了向网站的 OP 和未来用户表明这一点。