基于用户输入(行数)以菱形图案打印斐波纳契数列。 用户将输入的行数和输出应该包含这些多行打印斐波纳契数列如何打印菱形图案中的斐波那契数字
import java.util.*;
public class HelloWorld {
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
//Taking noOfRows value from the user
System.out.println("Enter no of rows:");
int noOfRows = sc.nextInt();
//Initializing rowCount with 1
int rowCount = 1;
//Implementing the logic
int previous = 0, prevtoprev;
for (int i = noOfRows; i > 0; i--) {
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++) {
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++) {
if (j%2 == 0) {
System.out.print("+");
} else {
System.out.print(j);
}
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--) {
if (j%2 == 0) {
System.out.print("+");
} else {
System.out.print(rowCount);
}
}
System.out.println();
//Incrementing the rowCount
previous = rowCount;
rowCount++;
}
}
}
获取输出
预期输出:
这里的“34 “被拆分以将”4“放置到下一行中以形成钻石,并且”144“被拆分以将”1“放置在最后一行中并且剩余的数字”44“被丢弃。
为什么很少有人给予否定检查 – Girish
预期产出是多少? –
请[编辑]问题。通过选择并单击“{}”按钮来正确地设置代码的格式。解释什么是“菱形格式”,以及您的预期输出是什么,然后*提出问题*。 – RealSkeptic