美好的一天傢伙!所以我現在自學java。我現在正在回答的練習之一是創建一個程序Pyramid.java,它接受輸入N並打印一個每邊長度爲N的金字塔,如下所示:我的問題是,每次我將輸入置於命令行,中間的3個星號不會出現。如何在Java中使用astersik製作金字塔?
*
* *
* * *
* * * *
* * * * *
這裏是我的代碼
public class D2Q6 {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int k = 1;
for(int i = 1; i <= N; i++) {
for(int j = 0; j < N-i; j++)
System.out.print(" ");
System.out.print("*");
if(i > 1) {
if(i == N) {
for(int j = 0; j < i-1; j++)
System.out.print(" *");
}
else {
for(int j = 0; j < k; j++)
System.out.print(" ");
k = k+2;
System.out.print("*");
}
}
System.out.println();
}
}
這是一個**經典**編程練習。有[這個地方的答案](https://en.wikibooks.org/wiki/C_Programming/Beginning_exercises#Loops)。 – karlphillip 2014-09-01 22:47:36
[我如何用for循環創建完美的急劇三角形?](http://stackoverflow.com/questions/24156468/how-can-i-make-perfect-acute-triangle-with-for-loop) – 2014-09-01 23:46:15