java 为什么我收到“绑定必须为正”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33065644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I receiving "bound must be positive" error?
提问by Omar N
I am trying to write code that simulates a game of scrabble. I designed a class which should simulate a scrabble bag, and I am trying to test it by printing the tileID in main after choosing a random tile. Whenever I run the code though, I keep getting the following error:
我正在尝试编写模拟拼字游戏的代码。我设计了一个应该模拟拼字游戏袋的类,我试图通过在选择随机瓷砖后在 main 中打印 tileID 来测试它。每当我运行代码时,我都会收到以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at hw3.RandomBag.randomPick(RandomBag.java:39)
at hw3.RandomBag.main(RandomBag.java:59
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at hw3.RandomBag.randomPick(RandomBag.java:39)
at hw3.RandomBag.main(RandomBag.java:59
Can somebody tell me why I am receiving that error?
有人能告诉我为什么我会收到那个错误吗?
import java.util.*;
public class RandomBag<E> implements Iterable<E> {
// instance varibles
private List<E> bag; // arraylist as the container
private Random rand; // random number generator
// constructors
public RandomBag() {
bag = new ArrayList<E>();
rand = new Random();
}
public RandomBag(int seed) {
bag = new ArrayList<E>();
rand = new Random(seed);
}
// returns the size of the bag
public int size() { return this.bag.size(); }
public boolean isEmpty() { // returns true/false if the bag is/is not empty
if (this.bag.isEmpty())
return true;
else
return false;
}
// adds the parameter element in the bag
public void add (E element) {this.bag.add(element);}
// randomly selects an element using the random number generator 'rand' and removes that element from the bag, and returns the element
public E randomPick() {
int index = rand.nextInt(this.bag.size());
E tileID = bag.remove(index);
return tileID;
}
// obtains an iterator for the bag, and returns it
public Iterator<E> iterator() {
// traverse bag using an iterator
Iterator it = bag.iterator();
return it;
}
//**
//** main() for testing RandomBag<E>
//**
public static void main(String[] args) {
RandomBag bag = new RandomBag();
Object tileID = bag.randomPick();
System.out.println(tileID);
}
}
回答by Eran
If this.bag.size()is 0, you are passing an invalid argument to nextInt().
如果this.bag.size()是 0,则您将无效参数传递给nextInt()。
This is clearly stated in the Javadoc :
这在 Javadoc 中有明确说明:
n the bound on the random number to be returned. Must be positive.
n 要返回的随机数的界限。必须是积极的。
nextInt(n)returns a number between 0 and n-1. What do you expect nextInt(0)to return?
nextInt(n)返回一个介于 0 和 n-1 之间的数字。你期望nextInt(0)返回什么?
In your main method you are attempting to pick an element of an empty bag. It can't work. You should check the size of the bag before calling randomPick(). And randomPick()should probably throw an exception when the bag is empty.
在您的主要方法中,您试图选择一个空袋子的元素。它不能工作。你应该在打电话之前检查袋子的大小randomPick()。并且randomPick()当包为空时应该抛出异常。
public static void main(String[] args) {
RandomBag bag = new RandomBag();
Object tileID = null;
if (bag.size() > 0)
tileID = bag.randomPick();
System.out.println(tileID);
}
回答by thegauravmahawar
The error says it all:
错误说明了一切:
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
Whenever bound is zeroor negative, it is an illegal argument to rand.nextInt(). Make sure that this.bag.size()is never zero.
每当 bound 是zeroor 时negative,它就是 的非法参数rand.nextInt()。确保this.bag.size()永远不会zero。
回答by Romulo
The error is happening because of the Line 62in your class:
由于您班级中的第 62 行,发生错误:
int index = rand.nextInt(this.bag.size());
If you call this code with an empty bag, it will always return 0.
如果您使用空包调用此代码,它将始终返回 0。
Next step is checking line 387of the class java.util.Random:
下一步是检查类的第 387 行java.util.Random:
if (bound <= 0)
throw new IllegalArgumentException(BadBound);
Since we are always getting 0 from this.bag.size(), it will always throw an exception.
由于我们总是从 获得 0 this.bag.size(),所以它总是会抛出异常。
To make it work, make sure to add elements to the bag, as in:
要使其工作,请确保向包中添加元素,如下所示:
public static void main(String[] args) {
RandomBag bag = new RandomBag();
for (int i = 0; i < 10; i++) {
bag.add(i); // adding elements
}
Object tileID = bag.randomPick();
System.out.println(tileID);
}

