Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型不匹配:无法从int转换为int[]_Java_Arrays_Eclipse_Random - Fatal编程技术网

Java 类型不匹配:无法从int转换为int[]

Java 类型不匹配:无法从int转换为int[],java,arrays,eclipse,random,Java,Arrays,Eclipse,Random,对于这一行,我似乎无法解决一个问题。我已经找过了。 任务是实现一个应用程序,包括棒球运动员的球衣号码、击球平均数和步行次数。必须使用20列3行的二维数组。 我正在设置线,以使球员的击球次数以及结果的随机数。但我得到一个错误,无法从int转换为int[]。 我读过其他几个同样问题的问题,似乎没有一个对我有帮助。我不能理解他们,但我仍然无法解决这个问题。 非常感谢。 D import java.util.Random; 公共类统计 公共静态void main(字符串[]args) { int[][]

对于这一行,我似乎无法解决一个问题。我已经找过了。 任务是实现一个应用程序,包括棒球运动员的球衣号码、击球平均数和步行次数。必须使用20列3行的二维数组。 我正在设置线,以使球员的击球次数以及结果的随机数。但我得到一个错误,无法从int转换为int[]。 我读过其他几个同样问题的问题,似乎没有一个对我有帮助。我不能理解他们,但我仍然无法解决这个问题。 非常感谢。 D

import java.util.Random;
公共类统计
公共静态void main(字符串[]args)
{
int[][]数组={{1,2,3},
{ 4, 5, 6 },
{ 7, 8, 9 }, 
{ 10, 11, 12 },
{ 13, 14, 15 },
{ 16, 17, 18 },
{ 19, 20, 21 },
{ 22, 23, 24 },
{ 25, 26, 27 },
{ 28, 29, 30 },
{ 31, 32, 33 }, 
{ 34, 35, 36 }, 
{ 37, 38, 39 },
{ 40, 41, 42 },
{ 43, 44, 45 },
{ 46, 47, 48 },
{ 49, 50, 51 },
{ 52, 53, 54 },
{ 55, 56, 57 },
{ 58, 59, 60 } };
随机数=新随机数();
for(int row=0;row
我认为在给数组变量赋值之前,必须指定所有维度。例如,数组[0][1]而不是仅数组[0]

我认为在为数组变量赋值之前,必须指定所有维度。例如,使用数组[0][1]而不是仅使用数组[0]

,因为您使用的是二维数组

数组[0]是一个由3个元素组成的数组,因此如果您尝试执行数组[0]=0,则会出现错误,而这正是您的行尝试执行的操作。因为,数组[0]包含一个数组

你需要做什么

array[counter][0] = shortNumber;
array[counter][1] = battingAverage;
array[counter][0] = numWalks;

显然,这些值可能是随机数等。

因为您使用的是2d数组

数组[0]是一个由3个元素组成的数组,因此如果您尝试执行数组[0]=0,则会出现错误,而这正是您的行尝试执行的操作。因为,数组[0]包含一个数组

你需要做什么

array[counter][0] = shortNumber;
array[counter][1] = battingAverage;
array[counter][0] = numWalks;

显然,这些值可能是随机数等。

您需要访问这两个索引

array[ row ][ column ] = randomNumbers.nextInt(); //Error here
这也可以写成

int[] oneRow = array[row];
int oneCell = oneRow[column];
如果你把
int[]oneRow=array[row]在内部循环上方,您可以将内部循环中的所有
数组[行]
替换为
oneRow

此外,
计数器
将始终为
0
,每次在声明内部循环时,您都会将该值重置为0

您可能希望使用计数器,但将其置于内部循环之外

int counter = 0;
for (int column = 0; column < array[ row ].length; column++ )
{
     array[ row ][ counter ] = randomNumbers.nextInt() //Error here
int计数器=0;
对于(int column=0;column
您需要访问这两个索引

array[ row ][ column ] = randomNumbers.nextInt(); //Error here
这也可以写成

int[] oneRow = array[row];
int oneCell = oneRow[column];
如果将
int[]oneRow=array[row];
置于内部循环上方,则可以将内部循环中的所有
array[row]
替换为
oneRow

此外,
计数器
将始终为
0
,每次在声明内部循环时,您都会将该值重置为0

您可能希望使用计数器,但将其置于内部循环之外

int counter = 0;
for (int column = 0; column < array[ row ].length; column++ )
{
     array[ row ][ counter ] = randomNumbers.nextInt() //Error here
int计数器=0;
对于(int column=0;column
@Carlos 抱歉弄错了…我不小心用了c#语法 我使用Integer类只是为了说明为什么有时需要新关键字

The Best Approach is to declare the array and initialise it at runtime

class BaseBall {
Integer[][] array ;
int noOfBaseBallPlayers;

public BaseBall(int noOfBaseBallPlayers)
{
this.noOfBaseBallPlayers = noOfBaseBallPlayers;
this.array = new Integer[noOfBaseBallPlayers][3];
}

public void assignValues()
{

Random randomNumbers = new Random();

    for (int row = 0; row < noOfBaseBallPlayers; row++ )
     {
         for (int column = 0; column < 3 ; column++ )
         {
             array[row][coloumn] = new Integer(randomNumbers.nextInt())   
         }

     }


}

}
最好的方法是声明数组并在运行时初始化它
职业棒球{
整数[][]数组;
国际篮球运动员;
公共棒球(国际棒球运动员)
{
this.noofbaseballopers=noofbaseballopers;
this.array=新整数[noOfBaseBallPlayers][3];
}
公共值()
{
随机数=新随机数();
对于(int row=0;row
@Carlos 抱歉弄错了…我不小心用了c#语法 我使用Integer类只是为了说明为什么有时需要新关键字

The Best Approach is to declare the array and initialise it at runtime

class BaseBall {
Integer[][] array ;
int noOfBaseBallPlayers;

public BaseBall(int noOfBaseBallPlayers)
{
this.noOfBaseBallPlayers = noOfBaseBallPlayers;
this.array = new Integer[noOfBaseBallPlayers][3];
}

public void assignValues()
{

Random randomNumbers = new Random();

    for (int row = 0; row < noOfBaseBallPlayers; row++ )
     {
         for (int column = 0; column < 3 ; column++ )
         {
             array[row][coloumn] = new Integer(randomNumbers.nextInt())   
         }

     }


}

}
最好的方法是声明数组并在运行时初始化它
职业棒球{
整数[][]数组;
国际篮球运动员;
公共棒球(国际棒球运动员)
{
this.noofbaseballopers=noofbaseballopers;
this.array=新整数[noOfBaseBallPlayers][3];
}
公共值()
{
随机数=新随机数();
对于(int row=0;row
计数器
变量的作用是什么?因为现在它就坐在那里,让编译器生气。计数器变量的作用是什么?因为现在它就坐在那里,让编译器生气。什么?
数组[行,列]
?这是无效的Java!为什么要使用包装类
整数而不是
int
?什么?
数组[行,列]
?这是无效的Java!为什么要使用包装类