为什么我接受"约束必须是积极的"错误?

时间:2015-10-11 13:39:35

标签: java random

我正在尝试编写模拟拼字游戏的代码。我设计了一个应该模拟拼字游戏包的类,我试图通过在选择随机磁贴后在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

有人可以告诉我为什么会收到这个错误吗?

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

    }
}

3 个答案:

答案 0 :(得分:9)

如果this.bag.size()为0,则表示您将无效参数传递给nextInt()

这在Javadoc中有明确说明:

  

n要返回的随机数的界限。 必须为正

nextInt(n)返回0到n-1之间的数字。您希望nextInt(0)返回什么?

在你的主要方法中,你试图挑选一个空袋子的元素。它不起作用。在致电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);

}

答案 1 :(得分:1)

错误说明了一切:

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Random.java:388)

每当绑定为zeronegative时,它都是rand.nextInt()的非法参数。确保this.bag.size()永远不会zero

答案 2 :(得分:1)

由于您班级中的第62行,错误正在发生:

int index = rand.nextInt(this.bag.size());

如果您使用空包调用此代码,它将始终返回0.

下一步是检查班级java.util.Random第387行

if (bound <= 0)
        throw new IllegalArgumentException(BadBound);

由于我们总是从this.bag.size()得到0,所以它总是会抛出异常。

要使其正常工作,请务必向包中添加元素,如:

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

}