找不到符号类Random

时间:2015-07-13 08:45:30

标签: java random

当我运行此代码时,技术上应该没有任何问题,但我不断收到错误“无法找到符号随机” 我需要在代码的开头输入任何内容吗??

public class RandomNumbers {

    public static void main(String[] args) {
        Random r = new Random();
        System.out.println("First number: " + r.nextInt(100));
        System.out.println("Second number: " + r.nextInt(100));
        System.out.println("Third number: " + r.nextInt(100));
        System.out.println("Fourth number: " + r.nextInt(100));
        System.out.println("Fifth number: " + r.nextInt(100));
    }
}

3 个答案:

答案 0 :(得分:7)

您需要导入Random

import java.util.Random; // here

public class RandomNumbers {

    public static void main(String[] args) {
        Random r = new Random();
        System.out.println("First number: " + r.nextInt(100));
        System.out.println("Second number: " + r.nextInt(100));
        System.out.println("Third number: " + r.nextInt(100));
        System.out.println("Fourth number: " + r.nextInt(100));
        System.out.println("Fifth number: " + r.nextInt(100));
     }
}

答案 1 :(得分:2)

你导入了Random吗?如果你不只是把

import java.util.Random

在顶部

答案 2 :(得分:1)

在开头尝试导入java.util.Random