java error: double cannot be dereferenced
java error: double cannot be dereferenced
好的,所以我需要生成 1 到 100 之间的 7 个随机数,并将最大值打印在屏幕上。然而,当我编译这段代码时:
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();
random
是原始类型 double
,因此没有方法,也没有 nextDouble
。
我假设你想使用 Random
class.
你搞糊涂了。
此语句产生单个 double
值:
double random = Math.random() * 100 + 1;
如果你想要一个随机生成器,使用
Random random = new Random ();
然后 random.nextDouble()
会产生一个介于 0.0 和 1.0 之间的数字。
另一种方法是将对 random.nextDouble()
的调用替换为 Math.random()
。
好的,所以我需要生成 1 到 100 之间的 7 个随机数,并将最大值打印在屏幕上。然而,当我编译这段代码时:
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();
random
是原始类型 double
,因此没有方法,也没有 nextDouble
。
我假设你想使用 Random
class.
你搞糊涂了。
此语句产生单个 double
值:
double random = Math.random() * 100 + 1;
如果你想要一个随机生成器,使用
Random random = new Random ();
然后 random.nextDouble()
会产生一个介于 0.0 和 1.0 之间的数字。
另一种方法是将对 random.nextDouble()
的调用替换为 Math.random()
。