随机数Random报错 IllegalArgumentException: Bound must be positive

错误代码

    private static Random RANDOM = new Random();
    private static Integer randomOnce(Integer max, Integer min) {
        return RANDOM.nextInt(max) % (max - min + 1) + min;
    }

报错信息

java.lang.IllegalArgumentException: bound must be positive
	at java.util.Random.nextInt(Random.java:388)

解决方案

private static Random RANDOM = new Random();
    private static Integer randomOnce(Integer max, Integer min) {
        if (max<=0)
            throw new IllegalArgumentException("Random positive must be greater than 0");
        return RANDOM.nextInt(max) % (max - min + 1) + min;
    }

你可能感兴趣的:(异常处理,java,java)