英文:
How to calculate number from two Jtextfield?
问题
我正在设计一个食品订购系统,我需要获取顾客支付的金额输入,然后减去他们需要支付的总金额以计算找零。我的JTextField
不能显示正确的找零金额,它只显示0.0。我不确定我的代码有什么问题。希望你们能帮助我。非常感谢你们的帮助,谢谢!
public Cash() {
init();
btnPay.addActionListener(this);
setVisible(true);
}
public String returnChange1() {
double change = 0.00 ;
double custPay;
String total = lblDisplayTotal.getText();
double a=Double.parseDouble(total);
if (!(txtCustPay.getText().isEmpty())){
custPay = Double.parseDouble(txtCustPay.getText());
change = custPay - a;
}
return String.valueOf(change);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnPay)) {
returnChange1();
}
}
public void init() {
txtChange = new JTextField(returnChange1());
txtChange.setSize(150, 30);
txtChange.setLocation(150, 250);
add(txtChange);
}
英文:
I'm designing a food ordering system which I need to get the input of cash that customer paid, then minus with the total price they need to pay to calculate the change. My JTextField
can't show the correct answer for the change, it only show 0.0. I'm not sure what's the problem with my code. Hope that you all can help me. Appreciate your helps, thank you!
public Cash() {
init();
btnPay.addActionListener(this);
setVisible(true);
}
public String returnChange1() {
double change = 0.00 ;
double custPay;
String total = lblDisplayTotal.getText();
double a=Double.parseDouble(total);
if (!(txtCustPay.getText().isEmpty())){
custPay = Double.parseDouble(txtCustPay.getText());
change = custPay - a;
}
return String.valueOf(change);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnPay)) {
returnChange1();
}
}
public void init() {
txtChange = new JTextField(returnChange1());
txtChange.setSize(150, 30);
txtChange.setLocation(150, 250);
add(txtChange);
}
答案1
得分: 1
你没有将函数分配给文本字段。在按钮操作中,不要仅调用函数,而是应将函数分配给文本字段:txtChange.setText(returnChange1())
。此外,尝试在将文本转换为双精度时使用 try 和 catch:
try {
double a = Double.parseDouble(total);
} catch (NumberFormatException e) {
e.printStackTrace();
}
上述代码在用户错误输入非数字字符时非常有用。
public Cash() {
init();
btnPay.addActionListener(this);
setVisible(true);
}
public String returnChange1() {
double change = 0.00;
double custPay;
String total = lblDisplayTotal.getText();
double a = Double.parseDouble(total);
if (!(txtCustPay.getText().isEmpty())) {
custPay = Double.parseDouble(txtCustPay.getText());
change = custPay - a;
}
return String.valueOf(change);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnPay)) {
txtChange.setText(returnChange1());
}
}
public void init() {
txtChange = new JTextField(returnChange1());
txtChange.setSize(150, 30);
txtChange.setLocation(150, 250);
add(txtChange);
}
英文:
You are not assigning the function to the text field. In the button action, don't simply call the function, in this case what you should do is assign the function to the text field: txtChange.setText(returnChange1())
, Also try to put a try and catch where you convert the text to double:
try{
double a = Double.parseDouble(total);
}catch(NumberFormatException e){
e.printStackTrace;
}
The above code is useful when the user mistakenly enters a character that is not a number.
public Cash() {
init();
btnPay.addActionListener(this);
setVisible(true);
}
public String returnChange1() {
double change = 0.00;
double custPay;
String total = lblDisplayTotal.getText();
double a = Double.parseDouble(total);
if (!(txtCustPay.getText().isEmpty())) {
custPay = Double.parseDouble(txtCustPay.getText());
change = custPay - a;
}
return String.valueOf(change);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnPay)) {
txtChange.setText(returnChange1());
}
}
public void init() {
txtChange = new JTextField(returnChange1());
txtChange.setSize(150, 30);
txtChange.setLocation(150, 250);
add(txtChange);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论