用循环打印正方形

时间:2013-11-27 09:14:31

标签: java loops

//大学工作

您好,我正在尝试使用循环打印正方形,它还需要用户输入高度和宽度。 输出应该如下所示

....
.  .
....

任何帮助都会被抄送

import java.util.Scanner;
public class Ex1 {
    public static void main(String[] args) {

        Scanner input = new Scanner(System. in );
        System.out.print("Please enter the height of the box: ");
        int x = input.nextInt();

        System.out.println("Please enter a width for the box: ");
        int y = input.nextInt();
        drawbox(x, y);

    }

    static void drawbox(int x, int y) {

        for (int j = 0; j < y; j++) {
            System.out.println("*");

            System.out.println();
            for (int i = 0; i < x - 2; i++) {
                System.out.print("*");
                for (int z = 0; z < y - 2; z++) {
                    System.out.print(" ");
                }
                System.out.println("*");

                for (int i = 0; j < y; j++) {
                    System.out.println("*");
                }

            }
        }
    }
}

7 个答案:

答案 0 :(得分:2)

更改循环
for(int i = 0; j<y ; j++){
      System.out.println("*");
 }

 for(int j = 0; j<y ; j++){
      System.out.println("*");
 }

要绘制矩形,请更改您的绘图框代码,如:

static void drawbox(int x, int y) {
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
    for (int i = 0; i < x - 2; i++) {
        System.out.print("*");
        for (int j = 0; j < y - 2; j++) {
            System.out.print(" ");
        }
        System.out.println("*");
    }
    for (int i = 0; i < y; i++) {
        System.out.print("*");
    }
    System.out.println();
}

答案 1 :(得分:0)

更改

  for(int i = 0; j<y ; j++){
      System.out.println("*");
 }

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

答案 2 :(得分:0)

你可以使用它(见下文)。这将打印第一行和最后一行的*。对于中间行,它会打印一个星形,后跟空格并用星号关闭。它还有另一种方法allStars,它重用第一行和最后一行的代码。

static void drawbox(int height, int width){
    for (int i = 1; i <= height; i++) {
        if(i==1 || i==height){
            allStars(width);
        }else{
            System.out.print("* ");
            for(int k = 2; k < width; k++){
                System.out.print("  ");
            }
            System.out.print("*");
            System.out.print("\n");
        }
    }
}
static void allStars(int width){
    for(int k = 1; k <= width; k++){
        System.out.print("* ");
    }
    System.out.print("\n");
}

答案 3 :(得分:0)

当问题得到修复时,您的解决方案就足够了。但是把它作为简单的方法,希望你能从中得到一些想法

private static void printSquare(int width,int length){
      for(int i=0;i<width;i++){
          StringBuilder stringBuilder=new StringBuilder();
          stringBuilder.append("* ");
          for (int j=2;j<length;j++){
              if(i==0){
                stringBuilder.append("* ");
              }else if(i==width-1){
                stringBuilder.append("* ");
              }else {
                stringBuilder.append("  ");
              }
          }
          stringBuilder.append("* ");
          System.out.println(stringBuilder);
      }
}

printSquare(5,5);时,输出

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

答案 4 :(得分:0)

通过循环绘制方形的简单算法

public void draw(int length){
    for(int i = 0; i < length; i++){
        for(int j = 0;j < length; j++){
            if(i!= 0 && j > 0 && i!= length - 1 && j!= length-1){
                System.out.print(" ");
            }
            else System.out.print("*");
        }
        System.out.println();
    }
}

答案 5 :(得分:0)

试试这个:

import java.util.Scanner;

    public class Rectangle {
        public static void main(String args[]){
             Scanner input = new Scanner(System. in );
                System.out.print("Please enter the height of the box: ");
                int x = input.nextInt();

                System.out.print("Please enter a width for the box: ");
                int y = input.nextInt();
                drawbox(x, y);

            }

            static void drawbox(int x, int y) {

                for (int j = 0; j < y; j++) {
                    System.out.print("* ");
                }
                System.out.println();
                    for (int i = 0; i < x; i++) {
                        System.out.print("* ");
                        for (int z = 0; z < y - 2; z++) {
                            System.out.print("  ");
                        }
                        System.out.println("*");
                        }

                        for (int k = 0; k < y; k++) {
                            System.out.print("* ");
                        }

                }    

        }

输出:

Please enter the height of the box: 10
Please enter a width for the box: 10
* * * * * * * * * * 
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
*                 *
* * * * * * * * * * 

答案 6 :(得分:0)

尝试

公共类HelloWorld {

 public static void main(String []args){
    System.out.println("Hello World");
int row = 15;
int clmn =50;
for(int i= 0;i<row; i++ ){
    for(int j= 0;j<clmn; j++ ){
        if(i == 0 || i == row-1 ||j == 0 || j == clmn){
            System.out.print("*");
        }else{
            System.out.print(" ");
        }

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

}