简体   繁体   English

通过使用二维数组乘法表

[英]Multiplying table by using two dimensional arrays

I am trying to make a multiplication table from 1 to 10 by using two dimensional arrays. 我试图通过使用二维数组使乘法表从1到10。 When I run the program I get each value under each other, not a table. 当我运行程序时,我得到的是每个值,而不是一个表。 I get the first value as 1 and all others 0. I also get the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at summing.main(summing.java:46) Could you give some tips about the algorithm? 我将第一个值设为1,将所有其他值设为0。在线程“ main”中也收到错误Exception java.lang.ArrayIndexOutOfBoundsException:11 at summing.main(summing.java:46)您能否提供有关该算法的一些技巧?

public static void main( String[] args) 
{
    Scanner scan = new Scanner( System.in);
    int x,y,z;
    int[][] table = new int[11][11];
    table[0][0] = 0;
    table[0][1] = 1;
    table[0][2] = 2;
    table[0][3] = 3;
    table[0][4] = 4;
    table[0][5] = 5;
    table[0][6] = 6;
    table[0][7] = 7;
    table[0][8] = 8;
    table[0][9] = 9;;
    table[0][10] = 10;  
    table[1][0] = 1;
    table[2][0] = 2;
    table[3][0] = 3;
    table[4][0] = 4;
    table[5][0] = 5;
    table[6][0] = 6;
    table[7][0] = 7;
    table[8][0] = 8;    
    table[9][0] = 9;
    table[10][0] = 10;

    System.out.println( "Start of multiplication\n");

    for (x = 0; x <= 10; x++)
    {
        for (y = 0; y <= 10; y++)
        {
            table[x][y] = table[x][1] * table[y][1];
            System.out.println(table[x][y]);//This line has the error.
        }
        table[x][y] = table[x][1] * table[y][1];
        System.out.print(table[x][y]);  
    }

    System.out.println( "\nEnd of multiplication\n" );
}

The reason you see a series of lines rather than values across is that your inner for loop uses println , not print . 之所以看到一系列的行而不是整个值,是因为您的for循环内部使用了println而不是print The inner loop would use print , then the outer loop would use println to break lines. 内部循环将使用print ,然后外部循环将使用println来换行。

The reason you get an out-of-bounds exception is because of this: 出现越界异常的原因是因为:

for (y = 0; y <= 10; y++)
{
    table[x][y] = table[x][1] * table[y][1];
    System.out.println(table[x][y]);
}
table[x][y] = table[x][1] * table[y][1]; // <======= Here

After the loop, y has the value 11 , so indexing into the table with it will cause the out-of-bounds exception. 循环之后, y的值为11 ,因此使用它索引表将导致越界异常。 You don't want that line at all, or the one after it. 您根本不希望该行,也不需要其后的那一行。 After the inner loop, all you want is: 内循环之后,您只需要:

System.out.println();

...to start a new line. ...开始新的一行。

Re algorithm: You don't actually need a two-dimensional array (or any array at all) to do this. 重新算法:你实际上并不需要一个二维数组(或根本任何阵列)来做到这一点。 But if you use one, you don't need to pre-initialize it with anything, because you have x and y . 但是,如果使用一个,则不需要用任何东西对其进行初始化,因为您有xy Just assign the product of x and y to the relevant cell in the inner loop: 只需将xy的乘积分配给内循环中的相关单元格即可:

table[x][y] = x * y;

I am trying to make a multiplication table from 1 to 10... 我正在尝试制作一个从1到10的乘法表...

Then the bounds of your loops are wrong, as you're going from 0 to 10 (inclusive). 然后,您的循环边界是错误的,因为您是从0到10(含10)。 Instead, you'd want 0 (inclusive) to 10 (exclusive) and to add one to your x and y when doing the multiplication: 取而代之的是,您希望0(含)到10(不含),并在进行乘法时将xy加一:

int[][] table = new int[10][10];
// ...
for (x = 0; x < 10; ++x) {
    for (y = 0; y < 10; ++y) {
         // ...
        table[x][y] = (x + 1) * (y + 1);
    }
}

Hopefully that's enough to go on, I didn't want to actually write the code for you, as you'll learn more by working it out. 希望可以继续进行下去,我不想为您编写代码,因为您可以通过练习来学习更多信息。

First if all there is no need to start the loop from x=0 and y=0. 首先,如果没有必要从x = 0和y = 0开始循环。 Second print all the values after calculating the values in the table Third ArrayIndexOutOfBound was Because when the y loop completed the value of y is 11 and you were using the that value of y after the loop has finished. 在计算了表Third ArrayIndexOutOfBound中的值之后,第二次打印所有值是因为y循环完成时y的值为11,并且在循环结束后使用y的那个值。

public static void main( String[] args) 
{
Scanner scan = new Scanner( System.in);
int x,y,z;
int[][] table = new int[11][11];
table[0][0] = 1;
table[0][1] = 1;
table[0][2] = 2;
table[0][3] = 3;
table[0][4] = 4;
table[0][5] = 5;
table[0][6] = 6;
table[0][7] = 7;
table[0][8] = 8;
table[0][9] = 9;;
table[0][10] = 10;  
table[1][0] = 1;
table[2][0] = 2;
table[3][0] = 3;
table[4][0] = 4;
table[5][0] = 5;
table[6][0] = 6;
table[7][0] = 7;
table[8][0] = 8;    
table[9][0] = 9;
table[10][0] = 10;

System.out.println( "Start of multiplication\n");

for (x = 1; x <= 10; x++)
{
    for (y = 1; y <= 10; y++)
    {
        table[x][y] = table[x][0] * table[y][0];

    }
}

System.out.println();

for(x=1;x<=10;x++){
    for(y=1;y<=10;y++){
        System.out.print(table[x][y] + "\t");
    }
    System.out.println("\n");
}

System.out.println( "\nEnd of multiplication\n" );
}    

You can try this, if you cannot get the error in your code. 如果您无法在代码中得到错误,可以尝试一下。 But first try to find the solution using your code as @TJ Crowder said: 但是首先尝试使用您的代码找到解决方案,如@TJ Crowder所说:

public static void main( String[] args) 
{
   // Scanner scan = new Scanner( System.in);
    int x,y;
    int[][] table = new int[11][11];
    table[0][0] = 0;
    table[0][1] = 1;
    table[0][2] = 2;
    table[0][3] = 3;
    table[0][4] = 4;
    table[0][5] = 5;
    table[0][6] = 6;
    table[0][7] = 7;
    table[0][8] = 8;
    table[0][9] = 9;;
    table[0][10] = 10;  
    table[1][0] = 1;
    table[2][0] = 2;
    table[3][0] = 3;
    table[4][0] = 4;
    table[5][0] = 5;
    table[6][0] = 6;
    table[7][0] = 7;
    table[8][0] = 8;    
    table[9][0] = 9;
    table[10][0] = 10;

    int[][] output_table = new int[11][11];

    System.out.println( "Start of multiplication\n");

    for (x = 0; x < 10; x++)
    {
        System.out.println("Table of: "+table[x][0]);
        for (y = 0; y < 10; y++)
        {
            output_table[x][y] = table[0][x] * table[y][0];
            System.out.print(output_table[x][y]+"\t");//This line has the error.
        }

        output_table[x][y] = table[0][x] * table[y][0];
        System.out.println(output_table[x][y]);  
    }

    System.out.println( "\nEnd of multiplication\n" );
}

` `

for(int i = 0;i<11;i++){
table[0][i] = i+1;
    table [i] [0] = i+1;
}

is much more efficient than having that massive list of declarations 比拥有大量声明列表要有效得多

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

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