类型不匹配:无法从 int 转换为 int[]
我似乎无法专门解决这一行的问题。我已经找过了。 作业是实现一个棒球运动员球衣号码、击球率和上垒次数的应用程序。必须使用 20 列 3 行的二维数组。 我正在努力设置线条来为玩家的击球次数和结果生成随机数。但我收到无法从 int 转换为 int[] 的错误。 我读过其他几个有同样问题的问题,但似乎没有一个对我有帮助。我可能不理解他们,但我仍然无法解决问题。 谢谢你, D
import java.util.Random;
public class Stats
public static void main(String[] args)
{
int[][] array = { { 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 } };
Random randomNumbers = new Random();
for (int row = 0; row < array.length; row++ )
{
for (int column = 0; column < array[ row ].length; column++ )
{
int counter = 0;
array[ counter ] = randomNumbers.nextInt() //Error here
System.out.print( counter + "\t " + array[ counter ] + "\t");
counter++;
}
System.out.println(" ");
}
}
}
A question I cannot seem to solve specifically for this line. I have searched for it.
The assignment is to implement an application a baseball players Shirt number, batting average and number of walks. Must use a 2-D array of 20 columns and 3 rows.
I am working on setting up lines to make the random numbers for the player's Number of times at bat as well as results. Yet I get a error of unable to convert from int to int[].
I have read several other questions with the same problem and none seem to help me. I can be not understanding them but still I am unable to solve the Issue.
Thank you,
D
import java.util.Random;
public class Stats
public static void main(String[] args)
{
int[][] array = { { 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 } };
Random randomNumbers = new Random();
for (int row = 0; row < array.length; row++ )
{
for (int column = 0; column < array[ row ].length; column++ )
{
int counter = 0;
array[ counter ] = randomNumbers.nextInt() //Error here
System.out.print( counter + "\t " + array[ counter ] + "\t");
counter++;
}
System.out.println(" ");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要访问这两个索引。
这也可以写成
如果将
int[] oneRow = array[row];
放在内部循环上方,则可以将内部循环中的所有array[row]
替换为oneRow
。此外,
counter
将始终为0
,每次声明它时进入内循环时都将值重置为0。您可能想使用计数器,但将其放在内部循环之外。
You need to access both indexes.
this can also be written as
If you put
int[] oneRow = array[row];
above your inner loop you can replace allarray[row]
in the inner loop withoneRow
.Besides,
counter
will always be0
, you reset the value to 0 each time you enter the inner loop when you declare it.You might want to use counter, but put it outside the inner loop.
我认为在为数组变量分配任何值之前必须指定所有维度。例如 array[0][1] 而不是仅 array[0]
I think you have to specify all the dimension before assigning any value to array variable. For example array[0][1] instead of only array[0]
因为您使用的是 2d 数组
array[0] 是一个包含 3 个元素的数组,所以如果您尝试执行 array[0]=0 操作,则会出错,而这正是您的行正在尝试执行的操作。因为,array[0]包含一个数组。
显然你需要做的
这些值可能是随机的等。
because you are using 2d array
array[0] is an array of 3 elements, so if you try to do array[0]=0, this will error, and this is what your line is trying to do. Because, array[0] contains an array.
you need to do
obviously those values could be randoms etc.
@卡洛斯
抱歉,我犯了这个错误...我不小心使用了 C# 语法
我使用 Integer 类只是为了说明为什么有时需要 new 关键字......
@Carlos
Sorry for the mistake...i accidently used the c# syntax
i used the Integer Class just to show why new keyword is necessary sometimes...