Java 单击“签出”按钮时出现的总计问题

Java 单击“签出”按钮时出现的总计问题,java,Java,我正在做一个关于订购冰激凌的程序,用户首先应该在圆锥体或杯子(作为按钮)之间进行选择,当他单击按钮时,总计显示在文本字段中,然后他可以继续选择风味作为复选框,每当他添加额外的风味时,总计将添加,依此类推 问题是,当我按下“签出”按钮时,它会变为另一个值,而不是它应该是的总金额 public void actionPerformed(ActionEvent e){ String a = e.getActionCommand(); double ttt;

我正在做一个关于订购冰激凌的程序,用户首先应该在圆锥体或杯子(作为按钮)之间进行选择,当他单击按钮时,总计显示在文本字段中,然后他可以继续选择风味作为复选框,每当他添加额外的风味时,总计将添加,依此类推

问题是,当我按下“签出”按钮时,它会变为另一个值,而不是它应该是的总金额

 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
}

嗨,欢迎来到StackOverflow。快速看一眼,很明显,您将在单个动作上多次触摸
total
——首先是杯形或锥形动作,然后是选择了任何RB单选按钮,然后再次是选择了任何CB按钮。因此,据我所知,最好使用多种方法来处理事件,但是,我必须在使用之前定义这些方法,还是立即使用它们?我尝试应用您的代码,我认为触发的操作不是由方法处理的。在创建按钮并为按钮B1分配这些事件时,您必须指定要处理事件的方法。选择SeiceStreamType(此);你是说这样?很抱歉有很多问题,但这是我第一次知道事件可以通过我的多种方法处理!b1.addActionListener(新ActionListener(){public void actionPerformed(ActionEvent e){chooseCreamType(e);}});不幸的是,我可以找到一种方法来理解它并实施它,但它没有与我一起工作,但感谢迄今为止的帮助。。