letitgo歌词:java随机数问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/06 08:34:16
import java.util.*;
public class Random {
public static void main(String []arg){
Random rand=new Random();
int i=rand.nextInt();
int j=rand.nextInt();
System.out.println(i+" "+j);
}
}

提示

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method nextInt() is undefined for the type Random
The method nextInt() is undefined for the type Random

at Random.main(Random.java:5)

请问错在哪里?

因为你的class也是Random, 改成
public class Random {
public static void main(String []arg){
java.util.Random rand=new java.util.Random();
int i=rand.nextInt();
int j=rand.nextInt();
System.out.println(i+" "+j);
}
}

Random类中找不到 nextInt() 方法

用double random = Math.random();就可以获得一个随机数了。
在(0,1]之间