2014-09-24 92 views
0

所以我一直在試圖弄清楚這一點,但沒有取得成功。所以,我想創建一個打印下面三角形的程序。Java從右向左打印聲明

********** 
********* 
    ******** 
    ******* 
    ****** 
    ***** 
     **** 
     *** 
     ** 
     * 

這是我現在的代碼。

public class PatternC 
{ 
    public static void main(String[] args) 
    { 
     int len = 1; 
     int k; 

     System.out.println("**********"); 
     for (int row = 9; row > len; row--) 
     { 

      for(int i=0; i<row; i++) 
       System.out.print("*"); 
      System.out.println(""); 
     } 
     for (int end = 0; end<1; end++) 
     { 
      System.out.format("%10s", "*"); 
     } 
    } 

} 

但是程序打印出來了!

********** 
********* 
******** 
******* 
****** 
***** 
**** 
*** 
** 
     * 

我正確所做的一切,但我只需要對準中間恆星最初從右到左開始。如果你有任何想法如何做到這一點,請幫助我。我已經嘗試過打印格式,但它不起作用。如果你還可以徹底解釋它,我會非常感激。

+3

就在每行開頭添加空格正確的(增加)號。 – Dici 2014-09-24 21:47:07

回答

1

我的解決辦法是:

public static void main(String[] args) { 
     int size = 6; 
     for(int i = 0; i < size; i++){ 
      for(int j = 0; j < i; j++) 
       System.out.print(" ");    
      for(int j = 0; j < size - i; j++) 
       System.out.print("*");    
      System.out.println(); 
     } 
    } 

在這裏,我輸出我的空間,(在第一行0,去年大小 - 1), 然後大小 - 我的星星。 (在第一行的大小,在最後1);

輸出:

****** 
***** 
    **** 
    *** 
    ** 
    * 
+2

'j <= i'應該是'j Dici 2014-09-24 21:55:57

+0

ou,你的權利。我會修復它 – 2014-09-24 22:34:31

+0

非常感謝你,但我有點用不同的方式。你的工作也是! – 2014-09-25 23:41:21

3

可能的空間都扔你。

如何打印以下圖案?

********** 
X********* 
XX******** 
XXX******* 
XXXX****** 
XXXXX***** 
XXXXXX**** 
XXXXXXX*** 
XXXXXXXX** 
XXXXXXXXX* 

它基本上是:

Print 0 X's, then 10 *'s 
Print 1 X, then 9 *'s 
Print 2 X's, then 8 *'s 
    . 
    . 
    . 
Print 9 X's, then 1 * 

一旦你得到的是,在你的代碼中的空格字符替換爲「X」,和你有它。

0

玉傢伙我已經想通了......

public class PatternC 
{ 
    public static void main(String[] args) 
{ 
       for (int r = 0; r<10; r++) 
       { 
        for (int i = 0; i<10; i++) 
        { 
         if (i >= r) 
        { 
         System.out.print("*"); 
        } 
        else if (i < r) 
        { 
         System.out.print(" "); 
        } 

       } 
       System.out.println(); 
      } 

    } 

    } 

這是我得到的輸出!





****** 
***** 
    **** 
    *** 
    ** 
    *