所以我创建了一个扩展acm包的RandomGenerator类的新类!我编译时遇到错误: " RandomGeneratorExtended.java:3:错误:期望的类,接口或枚举 private rgen = RandomGenerator.getInstance();"
出了什么问题?
import acm.util.*;
private RandomGenerator rgen = RandomGenerator.getInstance();
public class RandomGeneratorExtended {
public int nextSquare(int n){
double root= Math.sqrt(n);
int lim = (int)root;
int square = rgen.nextInt(0,lim);
return square*square;
}
public int nextSquare(int low, int high){
double rootlow = Math.sqrt(low);
double roothigh = Math.sqrt(high);
int lowlim = (int)rootlow;
int highlim = (int)roothigh;
int square = rgen.nextInt(lowlim, highlim);
return square*square;
}
}
答案 0 :(得分:3)
将类中的随机生成器实例定义为实例变量:
public class RandomGeneratorExtended {
private RandomGenerator rgen = RandomGenerator.getInstance();//private to class instance
....