2014-10-02 102 views
1

這裏的形狀應該是什麼like打印了一行兩種形狀的程序:我需要幫助編寫使用嵌套循環

enter image description here

這是到目前爲止我的代碼:

public class Diamonds { 

    public static void main(String[] args) { 
     for (int i = 1; i < 10; i += 2) { 
      for (int j = 0; j < 9 - i/2; j++) { 
       System.out.print(" "); 
      } 

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

      System.out.print("\n"); 
     } 

     for (int i = 7; i > 0; i -= 2) { 
      for (int j = 0; j < 9 - i/2; j++) { 
       System.out.print(" "); 
      } 

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

      System.out.print("\n"); 
     } 
    } 
} 

我無法獲得第二個形狀

+0

你什麼輸出?你有沒有試過調試你的代碼? – kraskevich 2014-10-02 16:50:20

+0

我不想爲你實際做你的作業(我不能成像這是實際應用程序的一部分),但這裏有一件事我可以馬上看到。你有一個外部循環有兩個內部循環,第一個打印空間,第二個打印星號,然後外部循環放入一個換行符。這是行不通的,因爲你需要在每一行上打印兩組星號。我懷疑正確的代碼將有一個外部循環和四個內部循環。我希望這有幫助 – 2014-10-02 17:02:56

回答

0

通過檢查右側的形狀,我們可以注意到,對於每個在左邊形狀行個星號,正確的有10 - N,所以,把你的代碼和擴展它,我們可以得到:

public class Diamonds { 
    public static final String SPACE = "  "; 
    public static void main(String[] args) { 
     for (int i = 1; i < 10; i += 2) { 
      for (int j = 0; j < 9 - i/2; j++) 
       System.out.print(" "); 

      for (int j = 0; j < i; j++) 
       System.out.print("*"); 

      System.out.print(SPACE); 

      for (int j = 0; j < 10 - i; j++) 
       System.out.print("*"); 

      System.out.print("\n"); 
     } 

     for (int i = 7; i > 0; i -= 2) { 
      for (int j = 0; j < 9 - i/2; j++) 
       System.out.print(" "); 

      for (int j = 0; j < i; j++) 
       System.out.print("*"); 

      System.out.print(SPACE); 

      for (int j = 0; j < 10 - i; j++) 
       System.out.print("*"); 

      System.out.print("\n"); 
     } 
    } 
} 

如果我們提取一些常用代碼:

public class Diamonds { 
    public static final String SPACE = "  "; 
    public static void main(String[] args) { 
     for (int i = 1; i < 10; i += 2) { 
      drawLine(i); 

      System.out.print("\n"); 
     } 

     for (int i = 7; i > 0; i -= 2) { 
      drawLine(i); 

      System.out.print("\n"); 
     } 
    } 

    private static void drawLine(int i) { 
     for (int j = 0; j < 9 - i/2; j++) 
      System.out.print(" "); 

     for (int j = 0; j < i; j++) 
      System.out.print("*"); 

     System.out.print(SPACE); 

     for (int j = 0; j < 10 - i; j++) 
      System.out.print("*"); 
    } 
} 
0

試試這個:

public static void main(String[] args) { 

     for (int i = 9; i > 0; i -= 2) { 
      for (int j = 0; j < 9 - i/2; j++) 
       System.out.print(" "); 

      for (int j = 0; j < i; j++) 
       System.out.print("*"); 

      System.out.print("\n"); 
     } 
     for (int i = 2; i < 10; i += 2) { 
      for (int j = 0; j < 9 - i/2; j++) 
       System.out.print(" "); 

      for (int j = 0; j <= i; j++) 
       System.out.print("*"); 

      System.out.print("\n"); 
     } 

    } 

輸出:

 ********* 
     ******* 
     ***** 
     *** 
     * 
     *** 
     ***** 
     ******* 
    ********* 
1

爲了不破壞你對這個問題的樂趣,我會解釋你需要做什麼,而無需編寫任何代碼。

要獲得那裏的第二個形狀,您需要將兩個額外的嵌套for循環添加到您已有的兩個「外」循環中。

循環第三個將產生固定數量的空間。請注意,第一個形狀的右邊緣與第二個形狀的左邊緣之間的距離是恆定的,所以您的第三個循環將很容易編碼。

第四個循環將像第一個循環一樣循環,但它們會改變位置:第一個外循環的第一個內循環將成爲第二個外循環的第四個內循環,反之亦然。

0
public class ReverseDiamond { 

    public static void main(String[] ar) { 

     int rows = 10; 
     ReverseDiamond diamond = new ReverseDiamond();   

     for(int i = 0; i < rows; i++) 
      diamond.printLine(rows, i); 

     for(int i = rows - 2; i >= 0; i--) 
      diamond.printLine(rows, i);   
    } 



    private void printLine(int rows, int currRow) { 

     for(int space = 1; space <= currRow; space++) 
      System.out.print(" "); 

     for(int star = 1; star < 2 * (rows - currRow); star++) 
      System.out.print("*"); 

     System.out.println(); 
    } 
} 
0

你可能喜歡:

public class Diamonds { 
public static void main(String[] args) { 
    int totalStars = 9; 
    int rows = 9; 

    for (int r = 0,stars=-1,gap=totalStars; r < rows; r++ ) { 
     stars+= (r<=rows/2) ?2:-2; 
     gap=totalStars-stars; 

     printChars(' ', gap); 
     printChars('*', stars); 
     printChars(' ', gap); 
     int gap2=stars+1; 
     int stars2=gap+1; 
     printChars(' ', gap2); 
     printChars('*', stars2); 
     printChars(' ', gap2); 
     System.out.println(); 
    } 
} 

private static void printChars(char c ,int times) { 
    for (int i = 0; i < times; i++) { 
     System.out.print(c); 
    } 
} 
}