2012-03-28 92 views
1

我上打印準空方,看起來像下面的例子(在10個星號和10下降爲2列)工作:爪哇 - 使用打印的空方嵌套循環

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

我的代碼不能動態生成用戶輸入中指定的行數和列數的方塊(它可以處理10行和10列,但只要將數字更改爲20,星號的數量不會改變。是我的代碼:

String STAR = "*"; 
String star1 = "**********"; 
int MAX = 10; 
for (int row = 0; row <= MAX; row += 1) { 
    for (int col = 0; col <= MAX ; col += 10) { 
     if (row == 0 && col == 0) 
      System.out.println(star1); 
     if (row >= 1 && row <= 4) 
      System.out.println(STAR + "  " + STAR); 
     if (row == 10 && col == 10) 
      System.out.println(star1); 
    } 
} 

歡迎任何幫助/建議代碼的活力。

+0

您的代碼並不顯示在您捕捉用戶輸入。 – assylias 2012-03-28 17:50:35

+0

我建議你首先嚐試用你的調試器來調試你的程序。你的列是否有任何理由增加10? – 2012-03-28 17:51:13

+0

您在這裏硬編碼了這麼多的值,如果用戶提供十以外的任何東西,它將不起作用。您需要根據用戶輸入以及其他東西來更改star1的定義!什麼是行> = 1 &&行<= 1?這似乎是錯誤的,也不會爲十個工作。 – 2012-03-28 17:52:19

回答

0

看看你的嵌套循環:

for (int col = 0; col <= MAX ; col += 10) { 

所以當col是10,你真的纔剛剛迭代一次......你還不如沒有嵌套循環的。

此外,star1和帶空格的字符串文字都有固定數量的字符,這些字符與列數明確相關。

我假設這是家庭作業,所以我不會給任何更多的提示比下手,但希望這會讓你沿着正確的思路思考...

0

,則應該更換在您的兩個for循環中由MAX變量發生了3次10,因此當用戶定義另一個大小時,您的for循環將採用其輸入而不是10值。

0

另外看看你最後一條if語句,在那裏它說if (row == 10 && col == 10)並思考它一秒鐘。一旦你點擊了10行和10列,你就會打印出最後的水平線star1,而不管MAX的設置是什麼。

2
String star = "*"; 
String space = " "; 

int MAX = xxx; 

for (int row = 0; row < MAX; row++) { 
    for (int col = 0; col < MAX; col++) { 
    if (row == 0 || row == MAX - 1) { 
     System.out.println(star); 
    } else if (col == 0 || col == MAX - 1) { 
     System.out.println(star); 
    } else { 
     System.out.println(space); 
    } 
    } 
} 
0

上面提到的一樣,嵌套的for循環是不必要的,如果你打算在未來創造更大的矩形(不是說你將不得不而是儘量保持遠離嵌套的for循環可能是低效如果你可以的話)。相反,只需在循環開始之前和退出之後打印star1。循環的主體應該足夠簡單。希望這可以幫助。

0
class Square 
{ 

    public static void main(String[] args) 
    { 
     String tenStars="**********"; 
     String oneStar="*"; 
     int count=0; 

     System.out.println(tenStars); 
     count++; 
     while(count<=8) 
     { 
      System.out.println(oneStar+"  "+oneStar); 
      count++; 
     } 
     System.out.print(tenStars); 

    } 
} 
0

這應該工作

public static void hallowSquare(int side) 
{ 
    int rowPos, size = side; 

    while (side > 0) 
    { 

     rowPos = size; 

     while (rowPos > 0) 
     { 

      if (size == side || side == 1 || rowPos == 1 || rowPos == size) 
       System.out.print("*"); 

      else 
       System.out.print(" "); 
      rowPos--; 
     } 
     System.out.println(); 
     side--; 

    }  

} 
0

這裏的另一種解決方案,更通用的一個。它可以讓你創建的高度「h」空心矩形,寬爲「W」

 private static void hallowSquare(int h, int w) 
{ 
    for(int i=1; i<=h; i++) 
    { 
     for(int j=1; j<=w; j++) 
     { 
      if (j==1|| j==w || i==1 || i==h) 
      System.out.print("X"); 
      else 
       System.out.print(" "); 
     } 
     System.out.println(); 
    } 

} 
0

你可以使用這樣的事情有一個用戶輸入......這是工作

public static void drawSquare(int size) 
{ 
     for(int i=1; i<size ;i++) 
     System.out.print("*"); 
     System.out.println(""); 
     for(int i=0; i<50 ;i++) 
     { 
       System.out.print("*"); 
       for(int j =0; j<size-3; j++) 
        System.out.print(" "); 
       System.out.println("*"); 
     } 

     for(int i=1; i<size ;i++) 
       System.out.print("*"); 
} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    drawSquare(50); 
} 

你應該只創建一個班,把它放在你的課堂上,並運行它...我希望這會幫助你... ...

0
class Star8 
{ 
    public static void main(String[] args) 
    { 
     for(int i=1;i<=5;i++) 
     { 
      for(int j=1;j<=5;j++) 
      { 
       if(i==2||i==3||i==4) 
       { 
        System.out.print("* *"); 
        break; 
       } 
       else 
       { 
        System.out.print("*"); 
       } 
      }  
      System.out.println(); 
     } 
    } 
} 
+1

歡迎來到Stack Overflow!將來,請注意確保您的代碼使用正確的縮進格式進行了很好的格式化,沒有不必要的空白空間等。我做了一些更改以幫助解決。 – 2014-08-15 21:30:20

0

希望這有助於,簡化你的思想伴侶。考慮軸x和y並按照該邏輯工作。在你的循環中創建一個嵌套循環,在循環中傳遞行,在每個循環中循環顯示正方形大小的數字 ,並在嵌套循環打印「*」後打印一個空格。

> for (int b=0;b<ans*2-3;b++) 

此嵌套循環具有的最大值B,因爲:

記住,而烏爾印刷,每個「*」從其他用空格隔開,並記住u的只有計數之間的空間第一列和最後一列。意思是在x = 0和x =平方之間的所有空間 ,因此最大b應該是這兩個座標之間的空間。 哪些是:squareize * 2/2是用於增加的空格/-3/* -3,因爲你省略了第一個座標(x = 0),最後一個座標(x =四捨五入)前循環。

import java.util.Scanner; 
public class AsteriksSquare { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Scanner input= new Scanner(System.in); 
     int ans; 
     System.out.print("Enter the size of the side of the square: "); 
     ans=input.nextInt(); 
     String ast="*"; 
     if (ans>0 && ans<21){ 

      for(int i=0;i<=ans-1;i++){ 
       System.out.print("* "); 


     } 
      System.out.println(""); 
      for(int i=1;i<=ans-2;i++){ 
       System.out.print("*"); 
       for (int b=0;b<ans*2-3;b++){ 
        System.out.print(" "); 

       } 
       System.out.println("*"); 


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


     } 

} 
} 
} 
0
class square1 
    { 
    public static void main(String[] args) 
     { 
     String star = "*"; 
     String space = " "; 
     int MAX = 5; 
     for (int row = 0; row < MAX; row++) 
     { 
      for (int col = 0; col < MAX; col++) 
      { 
      if (row == 0 || row == MAX - 1) 
       { 
       System.out.print(star); 
       } else if (col == 0 || col == MAX - 1) 
       { 
       System.out.print(star); 
       } else { 
       System.out.print(space); 
       } 
      } 
     System.out.println(); 
     } 
    } 
    } 
+3

歡迎來到堆棧溢出,並感謝您的回答。這個問題有11個其他答案,以及發佈代碼,你可能想添加一些文字,解釋爲什麼你的解決方案是一個好的,比其他一些更好。您可以通過點擊答案下方的編輯按鈕並添加文本來完成此操作。這可能會給你一些支持或欽佩你的努力。 – 2015-05-31 09:38:50

+0

歡迎來到Stack Overflow。你能否請你的答案給出解釋爲什麼這個代碼回答這個問題?僅限代碼答案[不鼓勵](http://meta.stackexchange.com/questions/148272),因爲他們沒有教導解決方案。 – DavidPostill 2015-05-31 11:32:26

0

此代碼應該做的伎倆。

package javaPackage; 

public class Square { 

    public static void main(String [] args) 
    { 
     for (int i=0;i<=10;i++) 
     { 
      for (int j=0;j<=10;j++) 
      { 
       if(i==0||i==10){ 
        System.out.print("x"); 
       } 
       else if(j==0||j==10){ 
        System.out.print("x"); 
       } 
       else{ 
        System.out.print(" "); 
       } 
      } 
      System.out.println(); 
     } 
    } 
} 

如果解釋認爲,你是第一個和最後一行(i = 0,I = 10),它將填補與X行。否則,它只會在行的開始和結尾處打印一個x。

0

您可以使用以下兩種方法。

1)用最少的代碼行。

for (int i = 0; i <= 9; i++) { 
      if (i == 0 || i == 9) { 
       System.out.println("* * * *"); 
      } else { 
       System.out.println("*  *"); 
      } 
     } 

OR

2)隨着兩個幫助循環

for (int i = 0; i <= 9; i++) { 
       for (int j = 0; j <= 9; j++) { 
        if (i == 0 || i == 9) { 
         System.out.print("*"); 
        } else { 
         if (j == 0 || j == 9) { 
          System.out.print("*"); 
         } else { 
          System.out.print(" "); 
         } 
        } 
       } 
       System.out.println(); 
      } 

感謝, Stuti

0
import java.util.Scanner; 
class Star 
{ 
    public static void main(String...args) 
    { 
     Scanner sc = new Scanner(System.in); 
     System.out.print("Enter the row : "); 
     int row=sc.nextInt(); 
     System.out.print("Enter the column : "); 
     int column=sc.nextInt(); 
     for(int i=1;i<=column;i++) 
     { 
      System.out.print("*"); 
     } 
     for(int i=row-2;i>=1;i--) 
     { 
      System.out.println(); 
      System.out.print("*"); 
      for(int k=1;k<=column-2;k++) 
      { 
       if(i<1) 
      { 
       break; 
      } 
       System.out.print(" "); 

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

你能詳細說明一下嗎?它是什麼,它如何回答用戶的問題?將其編輯到答案中。 – Ares 2017-07-27 17:14:56

+0

@ Ares在上面的代碼中,用戶可以給行和列的大小打印*。 – Shubham 2017-08-08 20:42:29

-1

我希望下面的代碼可以幫助,非常使用簡單的編碼並且具有所需的結果。

a=eval(input('Provide the height of the box: ')) 
b=eval(input('Provide the width of the box: ')) 
d=a-2 
r=b-2 

if a >= 1: 
    print('*'*b) 
if a > 1: 
    for i in range(d): 
     print('*',end='') 
     for i in range(r): 
      print(' ',end='') 
     print('*') 
    print('*'*b,end='') 

結果爲:
The following result is for the height: 6 and width:15