ダイヤモンドパターンでフィボナッチシリーズを印刷するユーザー入力(行数)に基づいています。 ユーザーは、行の番号を入力し、出力がフィボナッチ数をダイヤモンドパターンで印刷する方法
期待出力としてフィボナッチ数列
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