2012-03-28 90 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