gpt4 book ai didi

java - 我正在努力正确格式化这个嵌套 for 循环的输出

转载 作者:行者123 更新时间:2023-12-02 09:30:08 24 4
gpt4 key购买 nike

输出应该如下所示:

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();
}

关于java - 我正在努力正确格式化这个嵌套 for 循环的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com