好的,所以我有這個問題,你可以在那裏得到超過你實際支付的小故障。這是一種很難解釋,當你加量,那麼首先這裏是我的代碼:我該如何阻止這個整數溢出?
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++;
但我不知道我會怎麼用,在該代碼。任何人都可以幫助我嗎?
如何使用「長」? – Kevin 2012-08-09 01:26:44
你可以簡化你的代碼,只包含相關的部分?現在大多數都是無關緊要的。如果你想要快速和高質量的迴應,不要浪費別人的時間。在你問任何問題之前,去http://sscce.org/並得到想法 – 2012-08-09 01:29:13