Michael Queue
Michael Queue

Reputation: 1400

using arrays for multiplication table in java

I am creating a program that will function like multiplication table flash cards. The objective is to pick two random numbers from 0-12 and ask the user to input the correct answer. My idea was to work with a multi-dimensional array to store the table and then use indexes in the array to generate the random "flash cards" for the user. My code below functions correctly, in that, it picks two random indexes and gives the answer, but, i'm not sure how i would pull the individual indexes that have been picked. For ex. the program might output 99 based on my code and it would be obvious that the numbers picked were 11 and 9, but, i am not sure how to return the multipliers. On a side note, whenever i declare an array and instantiate it in the same statement I get a warning. Is this a bad habit to get into?

int i, j;
int MDArray[][]=new int [13][13];
for(i=0; i<13; i++)
    for(j=0; j<13; j++)
        MDArray[i][j]=i*j;
System.out.println(Arrays.deepToString(MDArray));

int index1=(int)(Math.random()*13);
int index2=(int)(Math.random()*13);



int answer;
answer = MDArray[index1][index2];

Upvotes: 0

Views: 2520

Answers (1)

mdolbin
mdolbin

Reputation: 936

index1 and index2 are your multipliers, what's the problem?

You can print any primitive:

int i, j;
    int MDArray[][]=new int [13][13];
    for(i=0; i<13; i++)
        for(j=0; j<13; j++)
            MDArray[i][j]=i*j;
    int index1=(int)(Math.random()*13);
    int index2=(int)(Math.random()*13);



    int answer;
    answer = MDArray[index1][index2];
    System.out.println(index1+"x"+index2+" = "+answer);

You can also print them separately

System.out.println(index1);

Upvotes: 1

Related Questions