使用二维数组与嵌套循环打印等比数列表格的编程求助
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, columnjis 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
- Filling the Array:
- We first set the first element of each row
array[i][0]to2^iusingMath.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.
- We first set the first element of each row
- 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

