对于 Random 类型,方法 nextInt(int) 未定义 [英] The method nextInt(int) is undefined for the type Random

查看:298
本文介绍了对于 Random 类型,方法 nextInt(int) 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导入了java.util包,但是nextInt、nextBoolean、nextDouble等函数还是无法识别

I have imported the java.util package but still nextInt, nextBoolean, nextDouble and other functions are not recognisable

import java.util.*;

public class Random {

public static void main(String[] args){


    Random random = new Random();
    System.out.println(random.nextInt(10));
    System.out.println(random.nextBoolean());
    System.out.println(random.nextDouble());
    System.out.println(random.nextFloat());
    System.out.println(random.nextGaussian());
    byte[] bytes = new byte[10];
    random.nextBytes(bytes);
    System.out.printf("[");
    for(int i = 0; i< bytes.length; i++)
    {
        System.out.printf("%d ", bytes[i]);
    }
    System.out.printf("]\n");

      System.out.println(random.nextLong());  
  System.out.println(random.nextInt());

  long seed = 95;
  random.setSeed(seed);
  }
}

推荐答案

Your class Random shadows java.util.Random (你导入的分辨率低于本地类名称);您可以更改本地类名,也可以使用完全限定的类名(这也将消除对 import 的需要).喜欢,

Your class Random shadows java.util.Random (your import has lower resolution then the local class name); you can change the local class name or you could use fully qualified class name (which will also remove the need for the import). Like,

java.util.Random random = new java.util.Random();

这篇关于对于 Random 类型,方法 nextInt(int) 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆