Jazz Joel Alvarez Jazz Joel Alvarez - 7 months ago 11
Java Question

How to make this codes run.?

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]);
}


I would like to get the iCards and iNumInCards for using them in the 2d array that I created. How do I do it?

Answer
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;
    }
}

Basically your problem is that

  • you don't seem to understand linear program flow and control statements
  • I'm not sure you understand arrays
  • and I'm not sure you really understand what you're trying to do.

You should probably read about the basics of Java, as such: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/

Anyways, the code fragment I posted initializes the 2-dimensional array with random numbers, supposedly the way as you probably originally intended.