Java JButton 只需工作一次

Java JButton Need to work only one time

我不明白,我该怎么做,如果单击按钮,它只会检查一次玩家级别并升级。直到钱不够?

1.Try

 if(ae.getActionCommand().equals("Upgrade LVL")) {  
                if (car.Playerlevel == 1){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 1 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);
                        }
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 100", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }           
                if (car.Playerlevel == 2){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 2 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 200", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 3){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 3 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 300", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 4){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 4 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 400", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 5){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 5 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 500", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 6){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 6 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 600", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 7){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 7 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 700", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 8){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 8 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 800", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 9){
                    if (car.money >= 2){
                        car.money -= 2;
                        car.Playerlevel +=1;
                        JOptionPane.showMessageDialog (null, "You have gained 9 Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}
                    if (car.money < 2){
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 900", "No Money", JOptionPane.ERROR_MESSAGE);}                
                    }
                if (car.Playerlevel == 10){
                        JOptionPane.showMessageDialog (null, "You have gained MAX Player Level", "Congralations", JOptionPane.INFORMATION_MESSAGE);}                            
            }

2.Try

if(ae.getActionCommand().equals("Upgrade LVL")) {           
            for (int i =0; i <11; i++){                 
                   if (car.Playerlevel == i){
                    if (car.money >= i*2){
                        car.money -= i*2;
                        car.Playerlevel +=1;                   
                        JOptionPane.showMessageDialog (null, "You have purchased " + i + " Player level.", "Congralations", JOptionPane.INFORMATION_MESSAGE);}                          
                    else{                           
                        JOptionPane.showMessageDialog (null, "You dont have enought money. This Upgrade costs 100*lvl", "No Money", JOptionPane.ERROR_MESSAGE);}               
                   }
            }
        }

这些尝试中没有一个是行不通的。按下时,我只需要按一次按钮即可。我试过刹车;但也没用。

实现此目的的一种简单方法是恢复循环。而不是

 for (int i =0; i <11; i++)

使用

 for (int i=10; i>= 0; i--)

问题是您在迭代时更改了 playerLevel 变量。我的上述想法之所以有效,是因为您只会增加水平而不会降低水平。更通用的方法是将结果存储在局部变量中,并在循环后使用它来设置 playerLevel。

实际上,如果您的循环没有您展示的更复杂,您也可以删除循环并使用 playerLevel 代替 i。