我编写了一个代码来使用for循环完成一个完美的三部曲。我如何使用更多for循环来创建这些三部曲中的三部曲?
注意:“ROptionPane”与“JOptionPane”相同
public static void main(String[] args) {
String rowsWantedAsSt = ROptionPane.showInputDialog(null, "How big would you like the Diamond to be?",
"Diamond", ROptionPane.QUESTION_MESSAGE);
int rowsWanted = Integer.parseInt(rowsWantedAsSt);
//stars on the top row
int starsWanted = 1;
//spaces on the top row
int spacesWanted = (rowsWanted * 2) + 3;
for (int rows = 0; rows < rowsWanted; rows++) {
for (int spaces = 0; spaces < spacesWanted; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < starsWanted; stars++) {
System.out.print("*");
}
System.out.println();
starsWanted += 2;
spacesWanted--;
}
starsWanted = 1;
spacesWanted = rowsWanted + 1;
for (int rows = 0; rows < rowsWanted; rows++) {
for (int spaces = 0; spaces < spacesWanted + 2; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < starsWanted; stars++) {
System.out.print("*");
}
for (double spaces = 0; spaces < (spacesWanted * 2) - 3; spaces++) {
System.out.print(" ");
}
for (int stars = 0; stars < starsWanted; stars++) {
System.out.print("*");
}
System.out.println();
starsWanted += 2;
spacesWanted--;
}
}
}
答案 0 :(得分:1)
Triforce与创建Sierpinski triangle的算法的第一次迭代结果相同。由Triforces组成的Triforce看起来像算法的第二次迭代。
Rosettacode.org有两个不同的Java implementations函数,它将迭代生成一个ASCII Sierpinski三角形;你可能想从那里开始。