好吧,所以我遇到了这个问题,你可能会因为实际支付而得到更多的故障。这很难解释,当你增加金额时,首先这是我的代码:
public void setAmount(Player player, int button) {
int amount = (Integer) player.getTemporaryAttribute("geAmount");
int id = (Integer) player.getTemporaryAttribute("geItem");
int price = (Integer) player.getTemporaryAttribute("price");
long totalPrice = price * amount;
switch(button) {
case 157:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(amount > 1) {
amount--;
} else {
amount = 1;
}
break;
case 159:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(amount < Integer.MAX_VALUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount++;
}
break;
case 162:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 1;
} else {
amount = 1;
}
break;
case 164:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 10;
} else {
amount = 10;
}
break;
case 166:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 100;
} else {
amount = 100;
}
break;
case 168:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 1000;
} else {
amount = player.getInventory().getContainer().getNumberOff(id);
}
break;
case 171:
case 173:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
player.getActionSender().sendConfig(1111, (Integer) player.getTemporaryAttribute("price"));
break;
}
if(amount == 0){
amount = 1;
}
if(amount >= Integer.MAX_VALUE || amount < 0) {
amount = Integer.MAX_VALUE;
}
player.setTemporaryAttribute("geAmount", amount);
player.getActionSender().sendConfig(1109, id);
player.getActionSender().sendConfig(1110, amount);
}
总价格是溢出的,但我不确定如何让它停止。有人建议使用这样的东西:
int last = 0;
int current = 0;
while(last < current) //loop terminates on overflow
last = current++;
但我不知道如何在此代码中使用它。有人可以帮帮我吗?
答案 0 :(得分:2)
答案 1 :(得分:0)
long totalPrice = price * amount;
将price
或amount
投射到long
,以便进行长时间的计算:
long totalPrice = (long)price * amount;
答案 2 :(得分:0)
将您的总数初始化为零...类似程序。转到http://codejava.co.uk/download_code.html