繁体   English   中英

数学“方程式”无法正常工作

[英]Math “equations” not working properly

有没有人曾经在加油站或杂货店使用过这些机器,你可以从中获得资金捐赠可回收物品? 好吧,我想制作一个虚拟的,到目前为止一切都没关系,直到我不得不做一些数学。 我只有13岁,所以即使我觉得这很简单,这部分也很棘手。 我需要可回收类型的价值乘以金额,然后加到总金额上。 但是,不是将其添加到总钱中,而是将总数改为我添加的最近值。 假设我添加2罐,即10美分,然后我再添加一罐,而不是总共15美分,我只需要5美分。 希望你明白。 我还想对我的代码进行一些建设性的批评。 我知道这不是最好的,但我刚开始学习java,所以任何帮助都会很可爱。

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    public class Machine {
    static JLabel label;
    static JComboBox typeList;
    static JComboBox amountList;
    public static void GUI(){

        JFrame frame = new JFrame("Recyclables Machine");
        frame.setVisible(true);
        frame.setSize(300,125);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);

        Integer[] amounts = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50};
        amountList = new JComboBox(amounts);
        panel.add(amountList);

        String[] types = {"Choose Recycable Type","Plastic Bottle","Can","2 Liter","Glass Bottle"};
        typeList = new JComboBox(types);
        panel.add(typeList);

        JButton button = new JButton("Add");
        panel.add(button);

        label = new JLabel("Total Money: 0 cents");
        panel.add(label);

        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                gettinItDone();
            }
        });
    }
    public static void gettinItDone(){
        String type = (String)typeList.getSelectedItem();
        int amount = (int)amountList.getSelectedItem();
        int money = 0;
        int temp = 0;

        if(type.equals("Plastic Bottle")){
            temp = 5 * amount;
            money = temp + money;
            label.setText("Total Money: "+ money +" cents");
        }else{
            if(type.equals("Can")){
                temp = 5 * amount;
                money = temp + money;
                label.setText("Total Money: "+ money +" cents");
            }else{
                if(type.equals("2 Liter")){
                    temp = 10 * amount;
                    money = temp + money;
                    label.setText("Total Money: "+ money +" cents");
                }else{
                    if(type.equals("Glass Bottle")){
                        temp = 10 * amount;
                        money = temp + money;
                        label.setText("Total Money: "+ money +" cents");
                    }else{
                        JOptionPane.showMessageDialog(null,"Invalid Recyclable Type", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }
    }
}

“money”变量的范围仅在事件侦听器触发时才有效。

button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            gettinItDone();
        }
    });

您需要将资金存储在#getIniitDone方法的范围之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM