繁体   English   中英

Java-使用嵌套循环打印空正方形

[英]Java - Printing an empty square using nested loops

我正在打印一个类似下面的示例的准空正方形(2列上有10个星号,下两列有10个星号):

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

我的代码无法根据用户输入的行数和列数动态生成平方(它适用于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);
    }
}

欢迎提供有关代码动态性的任何帮助/建议。

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);
    }
  }
}
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);

    }
}

这应该工作

    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--;

    }       

}

这是另一种解决方案,用途更广泛。 它使您可以创建一个高度为“ 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();
    }

}

您可以在一个用户输入中使用类似的东西...

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);
}

您应该只创建一个类,然后将其放入您的类中并运行它。希望对您有所帮助。

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();
         }
     }
 }

希望这会有所帮助,简化您的思想伴侣。 考虑一下x和y轴,并以此逻辑工作。 在ur for循环上制作一个嵌套循环,以循环通过线,在每种情况下,循环平方大小的数字并打印一个空格,然后在嵌套循环上打印“ *”。

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

此嵌套循环的最大值为b,因为:

请记住,当您进行打印时,每个“ *”之间的距离都是空格,并且请记住,您只是在计算第一列和最后一列之间的间隔。 意味着x = 0和x = squaresize之间的所有空间,因此max b应该是这两个坐标之间的空间。 它们是:squaresize * 2/2 用于添加的空间 / -3 / * -3因为u忽略了第一个坐标(x = 0),最后一个坐标(x = squaresize)和前一个循环添加的1个空间。

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("* ");


        }

}
}
}
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();
        }
     }
   }

这段代码可以解决问题。

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填充行。 否则,它只会在行的开头和结尾打印斧头。

您可以使用以下两种方法。

1)最少的一行代码。

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

要么

2)借助两个for循环

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

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("*");
        }
    }
}

查看您的嵌套循环:

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

因此,当col为10时,您实际上只需要迭代一次……您可能根本就没有嵌套循环。

此外, star1和带空格的字符串文字中都有固定数量的字符,这显然与列数有关。

我假设这是家庭作业,所以我不会给出比这更多的提示,但是希望这会让您沿着正确的方向思考...

您应该通过MAX变量在两个for循环中更改出现10的3次,因此当用户定义其他大小时,您的for循环将使用其输入值而不是10值。

还要看一下您的最后一个if语句,该语句在其中显示if (row == 10 && col == 10)然后再考虑一秒钟。 达到10行10列后,无论设置了什么MAX ,都将打印star1最终水平线。

就像上面提到的那样,嵌套的for循环是不必要的,并且如果您打算将来创建更大的矩形,效率可能会很低(不是说您必须这样做,而是尽量避免嵌套的for循环)。 相反,只需在循环开始之前和退出之后打印star1。 循环的主体应该足够简单。 希望这可以帮助。

我希望下面的代码可以帮助您,使用非常简单的编码并获得所需的结果。

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='')

结果是:
以下结果适用于高度:6和宽度:15

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM