简体   繁体   English

在Java中将数组用于乘法表

[英]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. 目的是从0-12中选择两个随机数,并要求用户输入正确的答案。 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. 该程序可能会基于我的代码输出99,很明显,选择的数字是11和9,但是,我不确定如何返回乘数。 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];

index1 and index2 are your multipliers, what's the problem? index1index2是您的乘数,这是什么问题?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM