2015-11-03 43 views
1

我怎樣才能得到這段代碼來正確顯示高度?所以如果鑽石星號的寬度是3,高度是5,那麼鑽石看起來就是這樣。Java鑽石的高度Asterisk

 * 
    *** 
    *** 
    *** 
    * 

它只是在中間增加了多餘的高度。這是我的代碼到目前爲止。它可以做的很好的寬度。

public static void main(String[] args) { 
    int width, height, heightT; 
    Scanner scan = new Scanner(System.in); 

    while(true) { 
     System.out.println("Enter width of diamond (odd number between 0-19): "); 
     width = scan.nextInt(); 
     if (width % 2 != 0) { 
      if (0 <= width && width <= 19) { 
       break; 
      } 
     } 
    } 
    while(true) { 
     System.out.println("Enter height of diamond (greater than or equal to width): "); 
     height = scan.nextInt(); 
     if(height >= width) { 
      heightT = height - width; 
      break; 
     } 
    } 

    for(int i = 1; i < width + 1; 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 = width - 2; 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

其良好的指向問題直接 – SSH

+0

這是否意味着我做了正確的,或者我應該強調的是,我不能在我的代碼得到這個角色? @SSH – Armdev

+0

當你輸入3和5時,你的輸出是什麼樣的? – Alexander

回答

0

在解決這種類型的邏輯,你要經常看,你知道從未改變的事情。在這種情況下,3件事永不改變

1.第一行。總是等於一個星號。

2.中線。始終等於星號中的寬度。

3.最後一行。總是等於一個星號。

現在,拆分鑽石在兩個由中間行。請注意,總高度將始終爲頂部(或底部)乘以2加1(中間一排)的高度。你得到更多的東西少是這樣的:

for(int i = 0; ((i*2)+1) <= height; i++){ 
     //Print top 
    } 

    // Print middle row outside of loops or inside if you must. 

    for(int i = 0; ((i*2)+1) <= height; i++){ 
     //Print bottom. 
    } 

編輯:這隻會是鑽石,其中高度大於或等於寬度真。

編輯:此代碼解決您的問題(給定的高度大於或等於寬度)。但是,沒有正確處理width = 0width = 1。我相信這對你來說足夠微不足道。

public static void main(String[] args) { 
    int height = 9, width = 9; 
    int spaces = (width - 1)/2, asterisks = 1; 



    for(int i = 0;((i*2)) < height; i++){ 
     for(int ii = 0;ii < spaces;ii++){ 
      System.out.print(" "); 
     } 
     for(int ii = 0;ii < asterisks;ii++){ 
      System.out.print("*"); 
     } 
    spaces--; 
    if(asterisks < width)asterisks += 2; 
    System.out.println(); 
    } 

    spaces = 0; 

    for(int i = 0; ((i*2)+1) < height; i++){ 
     if(width < height){ 
      for(int ii = 0;ii < spaces;ii++){ 
       System.out.print(" "); 
      } 
      for(int ii = 0;ii < asterisks;ii++){ 
       System.out.print("*"); 
      } 
     }else{ 
      for(int ii = 0;ii < (spaces + 1);ii++){ 
       System.out.print(" "); 
      } 
      for(int ii = 0;ii < (asterisks - 2);ii++){ 
       System.out.print("*"); 
      } 
     } 
    spaces++; 
    asterisks -= 2; 
    System.out.println(); 
    } 
} 
+0

我仍然困惑。我在頂部,底部和中間部分填充什麼 – Armdev

+0

@Armdev其他for-loops打印星號。這些for-loops將處理每一行以「跟蹤」我們必須打印多少(這是高度限制)。 – Alexander

+0

你可以給我的代碼?我的思想現在正在消失。或者使用我的代碼的一部分來實現? – Armdev

0

您可以計算高度和寬度之間的差異,並在while循環中重新使用您的邏輯。

for(int i = 1; i < width + 1; 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"); 
} 
int dif = height-width; 
while(dif !=0){ 
for(int i = width; i < width + 1; 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"); 
} 
dif--; 
} 
for(int i = width - 2; 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"); 
}