输出应该看起来像这样:

1 1 2

3 5 8

13 21 34


但是我当前的输出是:1 1 2 3 5 8 13 21 34

我应该如何处理格式?

rows = s.nextInt();
columns = s.nextInt();
int firstNumber = 0;
int secondNumber = 1;
for (int i = 0; i < rows; i++) {
    for(int j = 0; j < columns; j++) {
        int f = firstNumber + secondNumber;
        firstNumber = secondNumber;
        secondNumber = f;

        System.out.printf(f + " ");

    }
}

最佳答案

只需在外循环中调用println()(仅用于行)。

rows = s.nextInt();
columns = s.nextInt();
int firstNumber = 0;
int secondNumber = 1;
for (int i = 0; i < rows; i++) {
    for(int j = 0; j < columns; j++) {
        int f = firstNumber + secondNumber;
        firstNumber = secondNumber;
        secondNumber = f;
        System.out.printf(f + " ");
    }
    System.out.println();
}

10-06 02:10