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

发布于 2024-12-15 02:49:38 字数 1514 浏览 0 评论 0原文

我似乎无法专门解决这一行的问题。我已经找过了。 作业是实现一个棒球运动员球衣号码、击球率和上垒次数的应用程序。必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对岸观火 2024-12-22 02:49:38

您需要访问这两个索引。

array[ row ][ column ] = randomNumbers.nextInt(); //Error here

这也可以写成

int[] oneRow = array[row];
int oneCell = oneRow[column];

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

此外,counter将始终为0,每次声明它时进入内循环时都将值重置为0。

您可能想使用计数器,但将其放在内部循环之外。

int counter = 0;
for (int column = 0; column < array[ row ].length; column++ )
{
     array[ row ][ counter ] = randomNumbers.nextInt() //Error here

You need to access both indexes.

array[ row ][ column ] = randomNumbers.nextInt(); //Error here

this can also be written as

int[] oneRow = array[row];
int oneCell = oneRow[column];

If you put int[] oneRow = array[row]; above your inner loop you can replace all array[row] in the inner loop with oneRow.

Besides, counter will always be 0, 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.

int counter = 0;
for (int column = 0; column < array[ row ].length; column++ )
{
     array[ row ][ counter ] = randomNumbers.nextInt() //Error here
单身情人 2024-12-22 02:49:38

我认为在为数组变量分配任何值之前必须指定所有维度。例如 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]

只等公子 2024-12-22 02:49:38

因为您使用的是 2d 数组

array[0] 是一个包含 3 个元素的数组,所以如果您尝试执行 array[0]=0 操作,则会出错,而这正是您的行正在尝试执行的操作。因为,array[0]包含一个数组。

显然你需要做的

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

这些值可能是随机的等。

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

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

obviously those values could be randoms etc.

空袭的梦i 2024-12-22 02:49:38

@卡洛斯
抱歉,我犯了这个错误...我不小心使用了 C# 语法
我使用 Integer 类只是为了说明为什么有时需要 new 关键字......

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())   
         }

     }


}

}

@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...

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())   
         }

     }


}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文