You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用二维数组与嵌套循环打印等比数列表格的编程求助

Fixing Your Geometric Sequence Table Code

Hey there! I see you're trying to create a table of geometric sequences where each row starts with the next element from the previous row's sequence. Let's walk through what's wrong with your current code and how to fix it.

What's Off in Your Current Code

Your assignment line array[i][j] = (i+i) + (j+j); is calculating 2i + 2j, which has nothing to do with the geometric sequence pattern you want. That's why your output isn't matching your expected format.

The Correct Logic

The pattern you want follows a simple rule:

  • The element at row i, column j is 2 raised to the power of (i + j).
    • For row 0 (first row), j ranges from 0 to 9: 2⁰=1, 2¹=2, ..., 2⁹=512 (matches your first line)
    • For row 1, j ranges from 0 to9: 2¹=2, 2²=4, ..., 2¹⁰=1024 (matches your second line's start)
  • Alternatively, you can build each row by starting with the first element of the row (which is 2^i) and multiplying by 2 for each subsequent column.

Fixed Code

Here's the corrected code that will produce your desired output:

public class GeometricSequenceTable {
    public static void main(String[] args) {
        int[][] array = new int[10][10];
        
        // Fill the 2D array with the geometric sequence values
        for (int i = 0; i < array.length; i++) {
            // First element of row i is 2^i
            array[i][0] = (int) Math.pow(2, i);
            for (int j = 1; j < array[i].length; j++) {
                // Each next element is double the previous one in the row
                array[i][j] = array[i][j-1] * 2;
            }
        }
        
        // Print the table
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Explanation

  1. Filling the Array:
    • We first set the first element of each row array[i][0] to 2^i using Math.pow() (cast to int since we're dealing with integers).
    • For each subsequent column in the row, we just multiply the previous column's value by 2—this keeps the geometric sequence going.
  2. Printing:
    • The print loop is similar to your original, but now the array holds the correct values, so it will output the table exactly as you expect.

If you want to avoid using Math.pow(), you can also initialize the first row manually and then build each row based on the one above:

public class GeometricSequenceTable {
    public static void main(String[] args) {
        int[][] array = new int[10][10];
        
        // Fill the first row
        array[0][0] = 1;
        for (int j = 1; j < array[0].length; j++) {
            array[0][j] = array[0][j-1] * 2;
        }
        
        // Fill remaining rows: each row starts with the second element of the row above
        for (int i = 1; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                // For row i, column j: take row i-1, column j+1
                if (j+1 < array[i-1].length) {
                    array[i][j] = array[i-1][j+1];
                } else {
                    // If we reach the end of the row above, multiply the last element by 2
                    array[i][j] = array[i][j-1] * 2;
                }
            }
        }
        
        // Print the table
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}

This second approach builds each row by shifting the previous row's elements to the left and adding a new element at the end (which is double the last element of the current row). Either version will work perfectly for your needs.

内容的提问来源于stack exchange,提问作者PJ Villaflor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:47:35