我有这段代码:
private void discountButtonActionPerformed(java.awt.event.ActionEvent evt) {
double purchasePrice, discount, discountAmount;
DecimalFormat x = new DecimalFormat("$##.00");
purchasePrice = Double.parseDouble(inputPurchase.getText());
discount = 0.1;
if (purchasePrice < 10) {
outputDiscountAmount.setText(x.format(0));
outputPrice.setText(x.format(purchasePrice));
} else {
outputDiscountAmount.setText(x.format(purchasePrice * discount));
discountAmount = purchasePrice - Double.parseDouble(outputDiscountAmount.getText());
outputPrice.setText(x.format(discountAmount));
//Not sure whether the discountAmount variable should be in the "else"
//brackets or at the top. If it is at the top nothing works.
//If where it is presently seen, outputDiscountAmount works, but outputPrice doesn't.
//Don't know what's wrong.
}
}
我正在使用NetBeans 8.2作为GUI,并在JButton中。
可以看到我的评论,outputPrice
无效。我没有看到任何错误
怎么了?
该代码应该为超过10美元的购买提供10%折扣的价格,并输出新的成本。如果购买低于10美元,没有折扣。
谢谢
答案 0 :(得分:1)
我们假设purchasePrice = 10.00:
outputDiscountAmount.setText(x.format(purchasePrice * discount));
现在outputDiscountAmount = $ 1.00。
discountAmount = purchasePrice - Double.parseDouble(outputDiscountAmount.getText());
此行抛出NumberFormatException,因为$ 1.00不是有效数字。
现在我们已经确定了问题,你应该能够做其余的事情。 如果您不确定how,这是一个有用的链接。
答案 1 :(得分:0)
您无法为数字解析无效字符(在本例中为'$')。您的代码失败了NumberFormatException,如另一个答案中所述。
查找下面的完整代码。在这里,折扣计算为单独的双变量,然后格式化以显示在折扣标签上。
package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PriceDiscountSwing implements ActionListener{
private JLabel outputDiscountAmount;
private JLabel outputPrice;
private JTextField inputPurchase;
private JButton discountButton;
JFrame frame = null;
public PriceDiscountSwing(){
initUI();
}
private void initUI(){
frame = new JFrame ();
inputPurchase = new JTextField(20);
outputDiscountAmount = new JLabel();
outputPrice = new JLabel();
JPanel panel = new JPanel();
panel.add(inputPurchase);
panel.add(outputDiscountAmount);
panel.add(outputPrice);
discountButton = new JButton("Find Discount");
discountButton.addActionListener(this);
frame.add(panel, BorderLayout.CENTER);
frame.add(discountButton, BorderLayout.SOUTH);
frame.setSize(500, 500);
frame.setVisible(true);
}
private void discountButtonActionPerformed(java.awt.event.ActionEvent evt) {
double purchasePrice, discount, discountAmount;
DecimalFormat x = new DecimalFormat("$##.00");
String priceEnterred = inputPurchase.getText();
if (priceEnterred.trim().length() > 0){
purchasePrice = Double.parseDouble(priceEnterred.trim());
discount = 0.1;
if (purchasePrice < 10) {
outputDiscountAmount.setText("Discount: " + x.format(0d));
outputPrice.setText("Price: " + x.format(purchasePrice));
} else {
double actualDiscount = purchasePrice * discount;
outputDiscountAmount.setText("Dicount: " + x.format(actualDiscount));
//discountAmount = purchasePrice - Double.parseDouble(outputDiscountAmount.getText());
discountAmount = purchasePrice - actualDiscount;
outputPrice.setText("Price: " + x.format(discountAmount));
//Not sure whether the discountAmount variable should be in the "else"
//brackets or at the top. If it is at the top nothing works.
//If where it is presently seen, outputDiscountAmount works, but outputPrice doesn't.
//Don't know what's wrong.
}
}else{
JOptionPane.showMessageDialog(frame, "Please enter some amount");
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(discountButton)){
discountButtonActionPerformed(e);
}
}
public static void main (String []args){
new PriceDiscountSwing();
}
}