(JAVA) 需要帮助创建这个乘法表

huangapple go评论82阅读模式
英文:

(JAVA) Need assistance on creating this multiplication table

问题

以下是翻译好的代码部分:

public static void main(String[] args) {
        
        System.out.println();
        int[][] smallTable = createMultiplicationTable(3);
        printMultiplicationTable(smallTable);
        
        System.out.println();
        int[][] largeTable = createMultiplicationTable(10);
        printMultiplicationTable(largeTable);
        
    }
    
    // 创建并返回一个有 n 行 n 列的乘法表
    public static int[][] createMultiplicationTable(int n) {
        int max = n;
        int[][] table = new int[max][max];
        for (int i = 1; i < max; i++) {
            for (int j = 1; j < max; j++) {
                table[i][j] = i * j;
            }
        }
        return table;
    }
    
    // 使用 %4d 打印乘法表中的每个值
    public static void printMultiplicationTable(int[][] table) {
        int max = table.length;
        for (int i = 0; i < max; i++) {
            for (int j = 0; j < max; j++) {
                System.out.printf("%4d", table[i][j]);
            }
            System.out.println();
        }
    }
}

如果你的代码输出的格式不正确,你可能需要检查你的循环边界。在 createMultiplicationTable 方法中,你使用的循环应该从索引 0 开始,因为数组的索引是从 0 开始计数的。同样,在 printMultiplicationTable 方法中,你也应该从索引 0 开始循环。这样应该能够得到正确的格式。

英文:

The current code I have is outputting an incorrect format of my intended multiplication table. It outputs the current table :
(JAVA) 需要帮助创建这个乘法表

public static void main(String[] args) {
        
        System.out.println();
        int[][] smallTable = createMultiplicationTable(3);
        printMultiplicationTable(smallTable);
        
        System.out.println();
        int[][] largeTable = createMultiplicationTable(10);
        printMultiplicationTable(largeTable);
        
    }
    
    //Create and return a multiplication table witn n rows and n columns
    public static int[][] createMultiplicationTable(int n) {
        int max = n;
        int[][]table = new int[max][max];
        for (int i = 1; i &lt; max; i++) {
            for (int j = 1; j &lt; max; j++) {
                table[i][j] = i * j;
            }
        }
        return table;
        
        
        
    }
    
    //Print multiplication table using %4d to print each value
    public static void printMultiplicationTable(int[][] table) {
        int max = table.length;
        for (int i = 0; i &lt; max; i++) {
            for (int j = 0; j &lt; max; j++) {
                System.out.printf(&quot;%4d&quot;, table[i][j]);
            }
            System.out.println();
        }
        
        
        
    }
}

The correct format is suppose to look as so:
(JAVA) 需要帮助创建这个乘法表

Where in my code am I creating an error?

答案1

得分: 0

当你像这样声明一个 int 数组:

int[][] table = new int[max][max];

所有元素都有默认/初始值为零,由于你只在循环中为大于一的索引 i、j 赋值,具有索引 0、i 或 0、j 的元素保持不变。像下面这样做一个小改变就能得到你想要的结果:

public static int[][] createMultiplicationTable(int n) {
    int max = n;
    int[][] table = new int[max][max];
    for (int i = 0; i < max; i++) {
        for (int j = 0; j < max; j++) {
            table[i][j] = (i+1) * (j+1);
        }
    }
    return table;
}
英文:

When you declare an int array as you did here:

int[][]table = new int[max][max];

all elements have a default/initial value of zero and since you only assign values to indices i,j greater than one in your for loop, the elements with indices 0,i or 0,j remain unchanged. A small change like below should give you the desired result

public static int[][] createMultiplicationTable(int n) {
    int max = n;
    int[][]table = new int[max][max];
    for (int i = 0; i &lt; max; i++) {
        for (int j = 0; j &lt; max; j++) {
            table[i][j] = (i+1) * (j+1);
        }
    }
    return table;
}

huangapple
  • 本文由 发表于 2020年10月8日 06:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定