2015-12-04 58 views
2

click to see image金字塔模式的

您好我是新來JAVA.I一直在練習,但被困在一個金字塔problem.Please幫助怎麼辦it.This應該是輸出時的行數,用戶應輸入= 5。

我曾嘗試自己,但可能只配備了#不是@

public static void main(String args[]){ 
    int i,j,k;int cnt=0; 

    for(i=1;i<=x;i++) 
    { 
     System.out.print(" "); 
     for(j=1;j<=x-i;j++) 
     { 
      System.out.print(" "); 
     } 

     for(k=i;k>0;k--) 
     { 
      if(cnt%2==0) 
      System.out.print(" # ");//even position 
      else 
      System.out.print(" @ ");//odd position 
      cnt++; 
     } 
     System.out.println(" "); 
    } 

} 

回答

2

更改for循環,以

for(j=1;j<=x-i;j++) 
    { 
     System.out.print(" "); 
    } 
    for(k=i;k>0;k--) 
    { 
     if(cnt%2==0) 
     System.out.print("#  ");//even position 
     else 
     System.out.print("@  ");//odd position 
     cnt++; 
    } 

輸出:

  # 
     @  # 
     @  #  @ 
    #  @  #  @ 
#  @  #  @  # 
+0

薩蒂亞感謝您的時間和help.thanks很多 –

+0

好吧,現在你看到顯示的輸出和生成的輸出不同s你可以幫助如何獲得這些空間,我會顯示修改後的程序 –

+0

產生的輸出在'#','@'之後給出一些額外的空間。 – Satya