gpt4 book ai didi

java - 我该如何阻止这个整数溢出?

转载 作者:行者123 更新时间:2023-12-02 07:38:34 24 4
gpt4 key购买 nike

好吧,所以我遇到了这个问题,您可能会出现错误,从而获得比实际支付的更多的东西。这有点难以解释,当你增加数量时,首先这是我的代码:

    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++;

但我不知道如何在这段代码中使用它。有人可以帮我吗?

最佳答案

关于java - 我该如何阻止这个整数溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11875508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com