gpt4 book ai didi

java - 列表已满时抛出IllegalArgumentException

转载 作者:行者123 更新时间:2023-12-03 08:50:07 25 4
gpt4 key购买 nike

由于某些原因,我在这些特定的行上不断出错。列表中确实有索引,但是由于某些奇怪的原因,当我尝试运行它时,它给了我这个错误。

这是它抛出的错误并说了几行。

Exception in thread "main" java.lang.IllegalArgumentException: bound must be 
positive
at java.util.Random.nextInt(Unknown Source)
at Room.getRandomEnemy(Room.java:95)
at Room.<init>(Room.java:32)
at Game.startGame(Game.java:102)
at Main.main(Main.java:9)

这是我的代码。
static ArrayList<String> enemys = new ArrayList<>(Arrays.asList("Red minion", "Blue Minion", "Purple Minion", "Blitz", "Katarina", "Green Minion")); 
private String contents;
static ArrayList<String> items = new ArrayList<>(Arrays.asList("Mana Potion", "Health Potion", "Pickaxe", "Cloth-Armor", "Cull"));

int randomIndex = rand.nextInt(enemys.size());
this.getRandomEnemy(enemys);
Room inhibitor1 = new Room("inhibitor1", inhibitor1Desc, inhibi1Exit,
console);
game.startGame();

最佳答案

@Lashane是对的!

enter image description here

如果您的敌人列表为空,它将返回0,因此Random.nextInt()将抛出IllegalArgumentException。

关于java - 列表已满时抛出IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43400111/

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