Java随机数数组错误

标签 java arrays random

我编写了一个小型 Java 应用程序来随机生成带有随机数的数组。这个类可以正常工作:

骰子.java

import java.util.*;

public class Dice {
    private int i = 10;
    private int value = 0;
    private Random rnd = new Random();

    public void roll_dice() {
        int g = i;

        value = rnd.nextInt(g); // Here is not make a  single error.
        if ((value != 0) & (value <= 6)) {
            System.out.println("Number is :" + value);
        } else {
            System.out.println(" Error number should be between 1 and 6. Try again");
        }
    }
}

但是,当我编译以下类时,出现以下错误:

Error cannot find symbol - method nextInt(int)

随机.java

import java.util.*;

public class Random {
    private int[] number;
    private int g;
    private Random rnd = new Random();

    public void Bicbig(int amount) {
        g = amount;

        if ((amount >= 1) & (amount <= 100)) {

        } else {
            System.out.println("Please Enter Between 1 and 100. Please try again .");
        }
    }

    public void generate() {
        number = new int[g];

        int u = rnd.nextInt(g); // Error cannot find symbol - method nextInt(int)
    }
}

为什么我会收到此异常?我该如何修复它?

最佳答案

您不应该将您的类命名为Random。它隐藏了java.util.Random。重命名后程序将编译

关于Java随机数数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27361935/

相关文章:

Java正则表达式创建问题

java - 尝试提取 jar 文件时出错

java - 使用 URI 模式的 Dropwizard 请求过滤器

arrays - 尼姆 : advantage of using array over seq?

ios - 在 UITableView 中正确显示 JSON 数据

php - in_array 的随机错误

c - 我该怎么做才能每次都使整数随机

java - 如何在不规则多边形的内部获取随机点?

c++ - 这个 C++ 猜谜游戏在语法上是否正确?

java - 如何在 J2ME 中实现 session 变量?