基於用戶輸入(行數)以菱形圖案打印斐波納契數列。 用戶將輸入的行數和輸出應該包含這些多行打印斐波納契數列如何打印菱形圖案中的斐波那契數字
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