好的,所以我需要生成 7 个 1 到 100 之间的随机数,并将最大值打印在屏幕上。但是,当我编译此代码时:
public class ArrayofTemperatures
{
public static void main(String[] args)
{
double [] temp = new double [7];
int index;
double max;
double random = Math.random() * 100 + 1;
temp[0] = random.nextDouble();
max = temp[0];
for (index = 1; index < temp.length; index++)
{
temp[index] = random.nextDouble();
if (temp[index] > max)
max = temp[index];
}
System.out.println("The highest score is: " + max);
}
}
我收到这两个错误:
ArrayofTemperatures.java:12: error: double cannot be dereferenced temp[0] = random.nextDouble();
ArrayofTemperatures.java:16: error: double cannot be dereferenced temp[index] = random.nextDouble();
最佳答案
你很困惑。
此语句生成一个 double
值:
double random = Math.random() * 100 + 1;
如果你想要一个随机生成器,请使用
Random random = new Random ();
然后random.nextDouble()
会产生一个0.0到1.0之间的数字。
另一种方法是用 Math.random()
替换对 random.nextDouble()
的调用。
关于java错误: double cannot be dereferenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33801248/