2015-10-19 72 views
0

我想用星號和它的行必須由用戶給出的菱形圖案。當線數爲偶數時,我有問題要製作鑽石。這是所有的代碼:偶數行的打印鑽石

import java.util.Scanner; 
public class DrawDiam { 
    public static void main(String[] args) { 
     System.out.println("Please give the number of lines"); 
     Scanner in = new Scanner(System. in); 
     int L = in .nextInt(); 
     if (L < 4) { 
      System.exit(0); 
     } else { 
      if ((L % 2) != 0) { 
       int add = 1; 
       int numOfSpaces = L/2; 
       for (int i = 1; i <= L; i++) { 
        for (int j = numOfSpaces; j >= 1; j--) { 
         System.out.print(" "); 
        } 
        for (int j = 1; j <= add; j++) { 
         System.out.print("*"); 
        } 
        System.out.println(); 
        if (i < (L/2 + 1)) { 
         add = add + 2; 
         numOfSpaces = numOfSpaces - 1; 
        } else { 
         add = add - 2; 
         numOfSpaces = numOfSpaces + 1; 
        } 
       } 
      } else { 
       int add = 1; 
       int numOfSpaces = L/2; 
       for (int i = 0; i <= L + 1; i++) { 
        for (int j = numOfSpaces; j >= 1; j--) { 
         System.out.print(" "); 
        } 
        for (int j = 1; j <= add - 2; j++) { 
         System.out.print("*"); 
        } 
        System.out.println(); 
        if (i < (L/2 + 1)) { 
         add = add + 2; 
         numOfSpaces = numOfSpaces - 1; 
        } else { 
         add = add - 2; 
         numOfSpaces = numOfSpaces + 1; 
        } 
       } 
      } 
     } 
    } 
} 

所以程序要求一個號碼> = 4,奇數部分是運行完美,但對於L = 6偶數部分是走出這樣的:

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

例如L = 6應顯示this

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

可以得出它到底應該是什麼樣子? L = 4的鑽石是一種奇怪的想象.. – ergonaut

+0

你會如何繪製偶數的對稱鑽石? –

+0

我編輯了這個問題以更好地理解它,謝謝! – kllbrd

回答

1

這是我的溶液。你需要測試你是否已經通過鑽石的中間。如果你在(我== L/2)中間的和不增加添加或numOfSpaces

public static void main(String[] args) 
    { 
     System.out.println("Please give the number of lines"); 
    Scanner in = new Scanner(System. in); 
    int L = in .nextInt(); 
    if (L < 4) { 
     System.exit(0); 
    } else { 
     if ((L % 2) != 0) { 
      int add = 1; 
      int numOfSpaces = L/2; 
      for (int i = 1; i <= L; i++) { 
       for (int j = numOfSpaces; j >= 1; j--) { 
        System.out.print(" "); 
       } 
       for (int j = 1; j <= add; j++) { 
        System.out.print("*"); 
       } 
       System.out.println(); 
       if (i < (L/2 + 1)) { 
        add = add + 2; 
        numOfSpaces = numOfSpaces - 1; 
       } else { 
        add = add - 2; 
        numOfSpaces = numOfSpaces + 1; 
       } 
      } 
     } else { 
      int add = 1; 
      int numOfSpaces = L/2; 
      for (int i = 0; i < L+1; i++) { 
       for (int j = numOfSpaces; j >= 1; j--) { 
        System.out.print(" "); 
       } 
       for (int j = 1; j <= add - 2; j++) { 
        System.out.print("*"); 
       } 
       System.out.println(); 
       if (i < (L/2)) { 
        add = add + 2; 
        numOfSpaces = numOfSpaces - 1; 
       } 

編輯您的else塊在這裏:

   else if (i > (L/2)) 
       { 
        add = add - 2; 
        numOfSpaces = numOfSpaces + 1; 
       } 

編輯完的!

   } 
     } 
    } 
} 
+0

我覺得我現在有點愚蠢,但真的是男人,你DA REAL MVP。非常感謝! – kllbrd

+0

沒問題。永遠願意幫助! –

0

不要忘記在結束時關閉掃描儀,需要檢測時,「我」是「L」的中間,並刪除圍在中間的兩條線中的一個(原因舉例4/2 = 2,所以中間是2和3)。

此代碼工作:

import java.util.Scanner; 
public class Main { 
    public static void main(String[] args) { 
     System.out.println("Please give the number of lines"); 
     Scanner in = new Scanner(System. in); 
     int L = in .nextInt(); 
     if (L < 4) { 
      System.exit(0); 
     } else { 
      int midp = (L/2)+1; 
      int midm = (L/2)-1; 
      if ((L % 2) != 0) { 
       int add = 1; 
       int numOfSpaces = L/2; 
       for (int i = 1; i <= L; i++) { 
        for (int j = numOfSpaces; j >= 1; j--) { 
         System.out.print(" "); 
        }  
        for (int j = 1; j <= add; j++) { 
         System.out.print("*"); 
        } 
        System.out.println(); 
        if (i < (L/2 + 1)) { 
         add = add + 2; 
         numOfSpaces = numOfSpaces - 1; 
        } else { 
         add = add - 2; 
         numOfSpaces = numOfSpaces + 1; 
        } 
       } 
      } else { 
       int add = 1; 
       int numOfSpaces = L/2; 
       for (int i = 0; i <= L + 1; i++) { 
        if(i != midm){ 
         for (int j = numOfSpaces; j >= 1; j--) { 
          System.out.print(" "); 
         } 
         for (int j = 1; j <= add - 2; j++) { 
          System.out.print("*"); 
         } 
         System.out.println(); 
         if(i == midp){ 

         } else if (i < (L/2 + 1)) { 
          add = add + 2; 
          numOfSpaces = numOfSpaces - 1; 
         } else { 
          add = add - 2; 
          numOfSpaces = numOfSpaces + 1; 
         } 
        } 
       } 
      } 
     } 
     in.close(); 
    } 
} 
+0

由於沒有打開System.in,因此不應關閉包裝System.in的掃描儀。如果你打開它,關閉它;如果你沒有打開它,請不要關閉它。 (儘管在實踐中,這裏不會引起問題。) –