2017-04-08 68 views
0

必須制定方法,將使用最大3的語句繪製此鑽石。我能夠使用例如只是「*」,但我應該如何使用3 for循環每行添加新的字母,我應該使用char數組來做到這一點?在此先感謝爪哇鑽石與不同字母的聲明

A 
    BBB 
    CCCCC 
DDDDDDD 
EEEEEEEEE 
DDDDDDD 
    CCCCC 
    BBB 
    A 

這是我目前可以做的:

static void diamond() { 
    int n= 9; 
    int a=5; 
    int c=1; 
    int d=0; 

    for(int i= 0; i<= n/2; i++){ 
     for(int j= 1; j<a; j++){ 

       System.out.print(" "); 
     } 
     for(int k=1; k<c; k++){ 
      System.out.print("*"); 
     } 

     for(int m= 0; m<=d; m++){ 
      System.out.print("*"); 
     } 

     c++; 
     d++; 

     a--; 
     System.out.println(); 
    } 
    int b=1; 
    int y=3 ; 
    int z= 2; 


    for(int i= 0; i<n/2+1; i++){ 
     for(int j=0; j<b; j++){ 
      System.out.print(" "); 
     } 
     for(int k= 0; k<=y; k++){ 
      System.out.print("*"); 
     } 

     for(int l= 0; l<= z; l++){ 
      System.out.print("*"); 
     } 
     System.out.println(); 
     z--; 
     b++; 
     y--; 
    } 

} 

輸出

* 
    *** 
    ***** 
******* 
********* 
******* 
    ***** 
    *** 
    * 
+0

一個char []會改善你的問題:) –

+4

快,讓你做了什麼 – funcoding

+0

如果輸出是真正固定的,你可以打印一些字符串文字: ) – 9000

回答

0

把它歸結爲循環3。

public static String generatDiamond(int n) { 
    String diamond = ""; 

    String spaces = ""; // used to store spaces 
    String str = ""; // string to be printed every line 
    char c = 'A'; 

    for (int i = n; i >= -n; i--) { 
     // there needs to be |i| spaces, and the char needs to be printed 2 * (n - |i|) + 1 times. 


     // generate spaces 
     for (int j = 1; j <= Math.abs(i); j ++) { 
      spaces += " "; 
     } 
     // add first spaces 
     str += spaces; 

     // add chars 
     for (int j = 1; j <= 2* (n - Math.abs(i)) + 1; j ++) { 
      str += c; 
     } 
     // add last spaces 
     str += spaces; 

     // add the line to the general shape 
     diamond += str + "\n"; 

     // clear variable 
     str = ""; spaces = ""; 

     // change the char 
     if (i > 0) 
      c++; 
     else 
      c--; 
    } 

    // shape is done, return it 
    return diamond; 
} 

public static void main(String[] args){ 
    // testing sizes 0 to 10 
    for (int i = 0; i <= 10; i++) { 
     System.out.println("n = " + i); 
     System.out.println(generatDiamond(i)); 
    } 

} 

輸出示例:

n = 0 
A 

n = 1 
A 
BBB 
A 

n = 2 
    A 
BBB 
CCCCC 
BBB 
    A 

n = 3 
    A 
    BBB 
CCCCC 
DDDDDDD 
CCCCC 
    BBB 
    A 

n = 4 
    A  
    BBB 
    CCCCC 
DDDDDDD 
EEEEEEEEE 
DDDDDDD 
    CCCCC 
    BBB 
    A  

n = 5 
    A  
    BBB  
    CCCCC 
    DDDDDDD 
EEEEEEEEE 
FFFFFFFFFFF 
EEEEEEEEE 
    DDDDDDD 
    CCCCC 
    BBB  
    A  

n = 6 
     A  
    BBB  
    CCCCC  
    DDDDDDD 
    EEEEEEEEE 
FFFFFFFFFFF 
GGGGGGGGGGGGG 
FFFFFFFFFFF 
    EEEEEEEEE 
    DDDDDDD 
    CCCCC  
    BBB  
     A  

n = 7 
     A  
     BBB  
    CCCCC  
    DDDDDDD  
    EEEEEEEEE 
    FFFFFFFFFFF 
GGGGGGGGGGGGG 
HHHHHHHHHHHHHHH 
GGGGGGGGGGGGG 
    FFFFFFFFFFF 
    EEEEEEEEE 
    DDDDDDD  
    CCCCC  
     BBB  
     A  

n = 8 
     A   
     BBB  
     CCCCC  
    DDDDDDD  
    EEEEEEEEE  
    FFFFFFFFFFF 
    GGGGGGGGGGGGG 
HHHHHHHHHHHHHHH 
IIIIIIIIIIIIIIIII 
HHHHHHHHHHHHHHH 
    GGGGGGGGGGGGG 
    FFFFFFFFFFF 
    EEEEEEEEE  
    DDDDDDD  
     CCCCC  
     BBB  
     A   

n = 9 
     A   
     BBB   
     CCCCC  
     DDDDDDD  
    EEEEEEEEE  
    FFFFFFFFFFF  
    GGGGGGGGGGGGG 
    HHHHHHHHHHHHHHH 
IIIIIIIIIIIIIIIII 
JJJJJJJJJJJJJJJJJJJ 
IIIIIIIIIIIIIIIII 
    HHHHHHHHHHHHHHH 
    GGGGGGGGGGGGG 
    FFFFFFFFFFF  
    EEEEEEEEE  
     DDDDDDD  
     CCCCC  
     BBB   
     A   

n = 10 
      A   
     BBB   
     CCCCC   
     DDDDDDD  
     EEEEEEEEE  
    FFFFFFFFFFF  
    GGGGGGGGGGGGG  
    HHHHHHHHHHHHHHH 
    IIIIIIIIIIIIIIIII 
JJJJJJJJJJJJJJJJJJJ 
KKKKKKKKKKKKKKKKKKKKK 
JJJJJJJJJJJJJJJJJJJ 
    IIIIIIIIIIIIIIIII 
    HHHHHHHHHHHHHHH 
    GGGGGGGGGGGGG  
    FFFFFFFFFFF  
     EEEEEEEEE  
     DDDDDDD  
     CCCCC   
     BBB   
      A  
+0

謝謝!第一個就足夠了,我想我明白了 – vucko95

0

可以有好幾個不同的解決方案..在你​​的代碼中絲毫變化您可以執行以下操作:

static void diamond() { 
    int n = 9; 
    int a = 5; 
    int c = 1; 
    int d = 0; 

    char p = 'A'; // defining a char.. instead of * we will print this 

    for (int i = 0; i <= n/2; i++) { 
     for (int j = 1; j < a; j++) { 

      System.out.print(" "); 
     } 
     for (int k = 1; k < c; k++) { 
      System.out.print(p); 
     } 

     for (int m = 0; m <= d; m++) { 
      System.out.print(p); 
     } 

     c++; 
     d++; 
     p++; // in means p will be the next char 
     a--; 
     System.out.println(); 
    } 
    int b = 1; 
    int y = 3; 
    int z = 2; 

    p--; // p will be the previous char 
    for (int i = 0; i < n/2 + 1; i++) { 
     p--; 
     for (int j = 0; j < b; j++) { 
      System.out.print(" "); 
     } 
     for (int k = 0; k <= y; k++) { 
      System.out.print(p); 
     } 

     for (int l = 0; l <= z; l++) { 
      System.out.print(p); 
     } 
     System.out.println(); 
     z--; 
     b++; 
     y--; 
    } 

}