0

好吧,所以我有这个问题,你可以得到比你实际支付的更多的钱。这有点难以解释,当你增加数量时,首先这是我的代码:

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

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

4

3 回答 3

2

使用BigInteger

http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html

于 2012-08-09T01:25:23.990 回答
0
long totalPrice = price * amount;

Cast priceor amountto longso 计算以 long 形式执行:

long totalPrice = (long)price * amount;
于 2012-08-09T01:32:48.930 回答
0

将您的总数初始化为零......类似的程序。转到http://codejava.co.uk/download_code.html

于 2012-08-09T01:33:54.250 回答