简体   繁体   English

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

[英]Math “equations” not working properly

Has anybody ever used those machines at a gas station or grocery store where you get money for donating your recyclables? 有没有人曾经在加油站或杂货店使用过这些机器,你可以从中获得资金捐赠可回收物品? Well, I wanted to make a virtual one of those and so far everything's okay until I had to do some math. 好吧,我想制作一个虚拟的,到目前为止一切都没关系,直到我不得不做一些数学。 I'm only 13, so this part was pretty tricky even though I thought it was gonna be simple. 我只有13岁,所以即使我觉得这很简单,这部分也很棘手。 I need the recyclable type's value times the amount and then added to the total money. 我需要可回收类型的价值乘以金额,然后加到总金额上。 But instead of adding it to the total money it seems to just change the total to the recent value I added. 但是,不是将其添加到总钱中,而是将总数改为我添加的最近值。 Let's say I add 2 cans, which is 10 cents, and then I add one more can after that, instead of having 15 cents total, I just have 5 cents. 假设我添加2罐,即10美分,然后我再添加一罐,而不是总共15美分,我只需要5美分。 Hopefully you understand. 希望你明白。 I would also like some constructive criticism about my code. 我还想对我的代码进行一些建设性的批评。 I know it's not the best, but I just started learning java, so any help would be lovely. 我知道这不是最好的,但我刚开始学习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);
                    }
                }
            }
        }
    }
}

The scope of your "money" variable is only active when the event listener fires. “money”变量的范围仅在事件侦听器触发时才有效。

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

You need to store your money outside of the scope of the #getIniitDone method. 您需要将资金存储在#getIniitDone方法的范围之外。

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

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