IllegalArgumentException:在使用Random.nextInt()方法时,Bound必须是正数(IllegalArgumentException: Bound must be p

系统教程 行业动态 更新时间:2024-06-14 16:57:18
IllegalArgumentException:在使用Random.nextInt()方法时,Bound必须是正数(IllegalArgumentException: Bound must be positive when using Random.nextInt() method) //in a method that checks players equipment to change damage and armor values //none of these min/max values should spit out anything less than 0 switch(weapon) { case"Knife": minDamage = 3; maxDamage = 12; break; case"Short Sword": minDamage = 6; maxDamage = 15; break; case"Long Sword": minDamage = 8; maxDamage = 20; break; case"Battle Axe": minDamage = 14; maxDamage = 28; break; case"Death's Blade": minDamage = 20;maxDamage = 60; break; } //armor switch(armor) { case"Rags": playerArmor = 0; break; case"Hide Armor": playerArmor = 5; break; case"Leather Armor": playerArmor = 8; break; case"Chainmail": playerArmor = 12; break; case"Platemail": playerArmor = 20; break; case"Death's Armor": playerArmor = 50; break; } //in a method that handles the combat //The problem is somewhere in the logic for the damageDealt integer. int damageDealt = rand.nextInt(maxDamage - minDamage + 1)+minDamage; int damageTaken = rand.nextInt(enemyDamage - playerArmor); if(damageDealt >= 0) { enemyHealth -=damageDealt; } else { damageDealt = 0; } if(damageTaken>0) { playerHealth -=damageTaken; } else { damageTaken = 0; }

所以这只是我正在完成的文本冒险中的几行代码。 有时候我会得到这个弹出来的错误,说IllegalArgumentException:在战斗中Bound必须对Java的random()方法有效。

代码的第一部分是处理玩家库存的方法的一部分。 请注意,武器的每个最大值都应大于各自的最小值。 第二代代码是战斗方法的一部分。 当玩家选择攻击时,这些线路就会运行。 据我所知,DamageDealt整数似乎是问题。

我很确定

int damageDealt = rand.nextInt(maxDamage - minDamage + 1)+minDamage;

应该工作,并永远不会给minminmage值以下的整数值,甚至为零。 但是,由于某种原因,有时它会发生,并且游戏会出现错误并给出错误代码。

如果我只使用maxDamage,这个问题不会发生,但这不是最佳的,因为那时超级大功率武器的伤害与起始武器一样小。

如果你们可以找出一个解决方案来修复我的代码,或者甚至有一种完全不同的方式来随机选择整数范围内的数字,我将不胜感激。

//in a method that checks players equipment to change damage and armor values //none of these min/max values should spit out anything less than 0 switch(weapon) { case"Knife": minDamage = 3; maxDamage = 12; break; case"Short Sword": minDamage = 6; maxDamage = 15; break; case"Long Sword": minDamage = 8; maxDamage = 20; break; case"Battle Axe": minDamage = 14; maxDamage = 28; break; case"Death's Blade": minDamage = 20;maxDamage = 60; break; } //armor switch(armor) { case"Rags": playerArmor = 0; break; case"Hide Armor": playerArmor = 5; break; case"Leather Armor": playerArmor = 8; break; case"Chainmail": playerArmor = 12; break; case"Platemail": playerArmor = 20; break; case"Death's Armor": playerArmor = 50; break; } //in a method that handles the combat //The problem is somewhere in the logic for the damageDealt integer. int damageDealt = rand.nextInt(maxDamage - minDamage + 1)+minDamage; int damageTaken = rand.nextInt(enemyDamage - playerArmor); if(damageDealt >= 0) { enemyHealth -=damageDealt; } else { damageDealt = 0; } if(damageTaken>0) { playerHealth -=damageTaken; } else { damageTaken = 0; }

So this is just a few lines of code from a Text adventure I'm finishing up. Sometimes I will get this bug that pops up saying IllegalArgumentException: Bound must be positive for java's random() method during the combat.

The first blurb of code is part of a method that handles the players inventory. Take note that every max value for the weapons is larger than the respective min value as they should be. The second blurb of code is part of the combat method. When the player chooses to attack, those lines are run. The damageDealt integer seems to be the issue as far as I can tell.

I'm fairly certain that

int damageDealt = rand.nextInt(maxDamage - minDamage + 1)+minDamage;

should work and never give an integer value below the minDamage value or even zero for that matter. But, for some reason sometimes it does and the game freaks out and gives that error code.

The issue doesn't occur if I only use maxDamage, but this isn't optimal because then a super high power weapon can do just as little damage as the starting weapon.

If you guys can figure out a solution that would fix my code or even a whole different way to handle randomly choosing a number in an integer range, I would greatly appreciate it.

最满意答案

以下行可能是你的错误的来源: int damageTaken = rand.nextInt(enemyDamage - playerArmor);

Random.nextInt()只会返回0(包含)和传入的参数(不包括)之间的值。 因此,如果enemyDamage - playerArmor解析为负值,则nextInt()将抛出IllegalArgumentException。

The following line may be the source of your error: int damageTaken = rand.nextInt(enemyDamage - playerArmor);

Random.nextInt() will only return a value between 0 (inclusive) and the parameter passed in (exclusive). Therefore, if enemyDamage - playerArmor resolves to a negative value, then nextInt() will throw an IllegalArgumentException.

更多推荐

本文发布于:2023-04-12 20:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/befdc53f1ea0effc52f27dda4fff45ea.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正数   方法   nextInt   Random   IllegalArgumentException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!