public static void main(String[] args)
{
String sInput1;
int iInput1, iInput2,iNum,iCards,iNumInCards;
Random Numbers = new Random();
int Number[][] = new int[10][4];
int Cards[] = new int[10];
int Slots[] = new int[9];
for (int y=0;y<=9;y++)
{
for (int z=0;z<=3;z++)
{
iCards = y;
iNumInCards = z;
}
}
for (int i = 0; i < 40; i++)
{
iNum = Numbers.nextInt(10) + 1;
// yung iNum yung 40 random numbers
}
Number[iCards][iNumInCards] = iNum;
System.out.println (Number[iCards][iNumInCards]);
}
我想获得iCards和iNumInCards,以便在我创建的2d数组中使用它们。我该怎么做?
答案 0 :(得分:3)
那些iCards
,iNumInCards
&amp;需要初始化iNum
(分配初始值)。声明变量时,将为变量分配内存。在将变量用于表达式之前,必须初始化此内存以包含一些确定的值。
陷阱可能性:
Random
来自util
包的课程尚未导入。
这是固定的:
public class Testing {
public static void main(String[] args)
{
String sInput1;
int iInput1, iInput2,iNum = 0,iCards = 0,iNumInCards = 0;
java.util.Random Numbers = new java.util.Random();
int Number[][] = new int[10][4];
int Cards[] = new int[10];
int Slots[] = new int[9];
for (int y=0;y<=9;y++)
{
for (int z=0;z<=3;z++)
{
iCards = y;
iNumInCards = z;
}
}
for (int i = 0; i < 40; i++)
{
iNum = Numbers.nextInt(10) + 1;
// yung iNum yung 40 random numbers
}
Number[iCards][iNumInCards] = iNum;
System.out.println (Number[iCards][iNumInCards]);
}
}
答案 1 :(得分:1)
int Number[][] = new int[10][4];
int Cards[] = new int[10];
int Slots[] = new int[9];
for (int y = 0; y < 10; y++)
{
for (int z = 0; z < 4; z++)
{
iCards = y;
iNumInCards = z;
iNum = Numbers.nextInt(10) + 1;
Number[iCards][iNumInCards] = iNum;
}
}
基本上你的问题是
您应该阅读有关Java的基础知识,因此: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/
无论如何,我发布的代码片段用随机数初始化二维数组,据说这可能就像你最初想要的那样。