gpt4 book ai didi

java - 单击“结帐”按钮时的总计问题

转载 作者:行者123 更新时间:2023-12-01 19:47:50 26 4
gpt4 key购买 nike

我正在做一个订购冰淇淋的程序,用户首先应该在圆锥体或杯子之间进行选择(作为按钮),当他单击按钮时,总数会显示在文本字段中,然后他可以继续选择口味作为复选框,每当他添加额外的 flavor 等时,总数就会添加

问题是当我按下“查看”​​按钮时,它会更改为另一个值,而不是应有的总计

 public void actionPerformed(ActionEvent e){
String a = e.getActionCommand();
double ttt;
if(a.equals("cone")){
total = (total +7);
t1.setText(Double.toString(total));

} else if(a.equals("cup")){
total = (total +5);
t1.setText(Double.toString(total));

}
if(RB3.isSelected()){

total = total +7;
t1.setText(Double.toString(total));

}
if(RB2.isSelected()){

total = (total +7);
t1.setText(Double.toString(total));

}if(RB1.isSelected()){

total = (total +7);
t1.setText(Double.toString(total));

}if(CB1.isSelected()){

total = (total +5);
t1.setText(Double.toString(total));

} if(CB2.isSelected()){

total = (total +5);
t1.setText(Double.toString(total));

} if(CB3.isSelected()){

total = (total +5);
t1.setText(Double.toString(total));

}if(a.equals("Clear")){
t1.setText(" ");
t2.setText("");
t3.setText("");
RB1.setSelected(false);
RB2.setSelected(false);
RB3.setSelected(false);
CB1.setSelected(false);
CB2.setSelected(false);
CB3.setSelected(false);
total = 0;

}if(a.equals("Apply Code")){

String c= t2.getText();
if(c.equals("DISC10")){
JOptionPane.showMessageDialog(null,"Code applied Successfully");
ttt=total-total*0.1;
t3.setText(Double.toString(ttt));

}
else if(c.equals("")){
t3.setText(Double.toString(total));
}
}
if(a.equals("Check Out")){

t3.setText(Double.toString(total));

即使我应用优惠券代码,总变化

最佳答案

正如我从代码中可以理解的那样,您试图在单个方法中处理不同的步骤,从而计算每个步骤的每个操作。

尝试并根据自己的方法区分步骤

例如:选择冰淇淋类型

void chooseIceCreamType(ActionEvent event) {
String a = event.getActionCommand();
if(a.equals("cone")){
total = (total +7);
t1.setText(Double.toString(total));
} else if(a.equals("cup")){
total = (total +5);
t1.setText(Double.toString(total));
}
}

用户选择冰淇淋类型并继续选择口味后,计算所选口味

void calculateSelectedFlavors(ActionEvent event) {
if(RB3.isSelected()){
total = total +7;
t1.setText(Double.toString(total));
} if(RB2.isSelected()){
total = (total +7);
t1.setText(Double.toString(total));
} if(RB1.isSelected()){
total = (total +7);
t1.setText(Double.toString(total));
} if(CB1.isSelected()){
total = (total +5);
t1.setText(Double.toString(total));
} if(CB2.isSelected()){
total = (total +5);
t1.setText(Double.toString(total));
} if(CB3.isSelected()){
total = (total +5);
t1.setText(Double.toString(total));
}
}

并在自己的事件方法上处理清除、应用代码和结账按钮

void handleOnClear(ActionEvent event) {
t1.setText(" ");
t2.setText("");
t3.setText("");
RB1.setSelected(false);
RB2.setSelected(false);
RB3.setSelected(false);
CB1.setSelected(false);
CB2.setSelected(false);
CB3.setSelected(false);
total = 0;
}

void handleOnCodeApplied(ActionEvent event) {
String c= t2.getText();
if(c.equals("DISC10")){
JOptionPane.showMessageDialog(null,"Code applied Successfully");
ttt=total-total*0.1;
t3.setText(Double.toString(ttt));
}else if(c.equals("")){
t3.setText(Double.toString(total));
}
}

void checkOut(ActionEvent event) {
t3.setText(Double.toString(total));
//handle checkout logic
}

关于java - 单击“结帐”按钮时的总计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59108890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com