我无法理解,我怎么能这样做,如果点击按钮,它只会让Playerlevel一次性升级并升级。直到没有钱?
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);}
}
}
}
这些尝试都没有,不起作用。按下时,我只需按一次按钮工作。我试着刹车;但它也没有奏效。
答案 0 :(得分:0)
实现这一目标的一种简单方法是恢复循环。而不是
for (int i =0; i <11; i++)
使用
for (int i=10; i>= 0; i--)
问题是你在迭代时改变了playerLevel变量。我的上述想法是有效的,因为你只会增加水平而不会减少它们。更通用的方法是将结果存储在局部变量中,并在循环后使用它来设置playerLevel。
实际上,如果你的循环并不像你展示的循环那么复杂,你也可以删除循环并使用playerLevel而不是i。