Java 获取错误,我可以';我不明白
下面的代码给了我一个“不能在原语类型double上调用nextDouble()”错误。我对Java相当陌生,有人能提供一些关于是什么导致它的指导吗Java 获取错误,我可以';我不明白,java,primitive-types,Java,Primitive Types,下面的代码给了我一个“不能在原语类型double上调用nextDouble()”错误。我对Java相当陌生,有人能提供一些关于是什么导致它的指导吗 public static double[][] getArray(int row,int column){ double [][] a = new double[row][column]; double input; for (int x=0; x<a.length; x++){ for (int
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
double input;
for (int x=0; x<a.length; x++){
for (int y=0; y<a[x].length; y++){
a[x][y] = input.nextDouble();
}
}
return a;
}
publicstaticdouble[]getArray(int行,int列){
double[]a=新的双精度[行][列];
双输入;
对于(int x=0;x您已经对变量input
使用了类型double
,并且无论如何您都不能对基元类型调用方法。
如果要从控制台扫描双人床,请按如下方式使用类型Scanner
:
Scanner input = new Scanner(System.in);
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
Scanner input = new Scanner(System.in);
for (int x=0; x<a.length; x++){
for (int y=0; y<a[x].length; y++){
a[x][y] = input.nextDouble();
}
}
return a;
}
因此,您的方法应该如下所示:
Scanner input = new Scanner(System.in);
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
Scanner input = new Scanner(System.in);
for (int x=0; x<a.length; x++){
for (int y=0; y<a[x].length; y++){
a[x][y] = input.nextDouble();
}
}
return a;
}
publicstaticdouble[]getArray(int行,int列){
double[]a=新的双精度[行][列];
扫描仪输入=新扫描仪(System.in);
对于(int x=0;x您已经对变量input
使用了类型double
,并且无论如何您都不能对基元类型调用方法。
如果要从控制台扫描双人床,请按如下方式使用类型Scanner
:
Scanner input = new Scanner(System.in);
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
Scanner input = new Scanner(System.in);
for (int x=0; x<a.length; x++){
for (int y=0; y<a[x].length; y++){
a[x][y] = input.nextDouble();
}
}
return a;
}
因此,您的方法应该如下所示:
Scanner input = new Scanner(System.in);
public static double[][] getArray(int row,int column){
double [][] a = new double[row][column];
Scanner input = new Scanner(System.in);
for (int x=0; x<a.length; x++){
for (int y=0; y<a[x].length; y++){
a[x][y] = input.nextDouble();
}
}
return a;
}
publicstaticdouble[]getArray(int行,int列){
double[]a=新的双精度[行][列];
扫描仪输入=新扫描仪(System.in);
例如(int x=0;xLook看看你是如何声明input
!input
是一个double。在double
中没有方法nextDouble()
。事实上,没有任何方法,因为它是一个原语。现在来看看教程:你想要的是随机输入=新随机();Scanner类有nextDouble()
请注意,输入未初始化。此外,double没有任何方法作为nextDouble()
。如果要生成随机双精度值,可以使用random
类。如果要接受用户提供的值,请使用Scanner类。看看如何声明input
!input
是双精度值。没有方法nextDouble()
在double
中。事实上没有任何方法,因为它是一个原语。现在开始学习教程:你想要的是随机输入=新随机();Scanner类有nextDouble()
注意你的输入没有初始化。此外,double没有任何方法作为nextDouble()
。如果要生成随机双精度值,可以使用random
类。如果要接受用户提供的值,请使用Scanner类。