【问题标题】:Print a rectangular in java [closed]在java中打印一个矩形[关闭]
【发布时间】:2020-02-08 20:39:17
【问题描述】:
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter width: ");
    int w = input.nextInt();
    System.out.print("Enter height: ");
    int h = input.nextInt();

    printHeight("x", h);
    printWidth("x", w);

}

private static void printHeight(String height, int count) {
    for (int i = 1; i <= count; i++) {
        System.out.println(height);
    }
}

private static void printWidth(String width, int count1) {
    for (int j = 1; j <= count1; j++) {
        System.out.print(width);
    }
}

我正在尝试打印具有输入高度和宽度的矩形,但它们显示出不同的模式。有没有办法嵌套它们?

【问题讨论】:

  • 如果我(例如)输入23 作为输入,输出应该是什么样子?
  • 1 行打印 xxx 作为宽度,2 行打印 xxx 和 xxx,因此总共 6x。
  • @Trung - 如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。接受的答案有助于未来的访问者自信地使用该解决方案。查看meta.stackexchange.com/questions/5234/… 了解如何操作。

标签: java rectangles


【解决方案1】:

您可以使用以下代码在控制台中绘制一个矩形:

package test;

public class Main {

    public static void main(String[] args) {
        printRect(5,10,'x');
    }

    private static void printRect(int width,int height,char marker) {
        printHorizontal(width,marker);
        for(int j=0;j<height-2;j++) {
            printVertical(width,marker);
        }
        printHorizontal(width,marker);
    }

    private static void printVertical(int width, char marker) {
        System.out.print(marker);
        for (int j = 0; j < width-2; j++) {
            System.out.print(' ');
        }
        System.out.println(marker);
    }

    private static void printHorizontal(int width,char marker) {
        for (int j = 0; j < width-1; j++) {
            System.out.print(marker);
        }
        System.out.println(marker);
    }

}

输出:

xxxxx
x   x
x   x
x   x
x   x
x   x
x   x
x   x
x   x
xxxxx

【讨论】:

    【解决方案2】:

    按如下方式进行:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter width: ");
            int w = input.nextInt();
            System.out.print("Enter height: ");
            int h = input.nextInt();
            System.out.println("A filled rectangle:");
            printFilledRectangle("x", w, h);
            System.out.println("An empty rectangle:");
            printEmptyRectangle("x", w, h);
        }
    
        private static void printFilledRectangle(String character, int width, int height) {
            for (int i = 1; i <= height; i++) {
                for (int j = 1; j <= width; j++) {
                    System.out.print(character);
                }
                System.out.println();
            }
        }
    
        private static void printEmptyRectangle(String character, int width, int height) {
            for (int j = 1; j <= width; j++) {
                System.out.print(character);
            }
            System.out.println();
            for (int i = 1; i <= height - 2; i++) {
                System.out.print(character);
                for (int j = 1; j <= width - 2; j++) {
                    System.out.print(" ");
                }
                System.out.println(character);
            }
            for (int j = 1; j <= width; j++) {
                System.out.print(character);
            }
        }
    }
    

    示例运行:

    Enter width: 10
    Enter height: 8
    A filled rectangle:
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    xxxxxxxxxx
    An empty rectangle:
    xxxxxxxxxx
    x        x
    x        x
    x        x
    x        x
    x        x
    x        x
    xxxxxxxxxx
    

    【讨论】:

      【解决方案3】:
      import java.util.Scanner;
      
      public class MethodsSecond {
          public static void main(String[] args) {
              Scanner input = new Scanner(System.in);
              System.out.print("Enter width: ");
              int w = input.nextInt();
              System.out.print("Enter height: ");
              int h = input.nextInt();
              System.out.println("");
              printRectangle("x" , w, h);
          }
      
          private static void printRectangle(String character, int width, int height) {
              for (int i = 1; i <= height; i++) {
                  for (int j = 1; j <= width; j++) {
                      System.out.print(character);
                  }
                  System.out.println();
              }
          }
      }
      

      非常感谢你们。我只是不明白嵌套循环。你介意分享一些关于进一步学习书籍的想法吗?我们课堂上的讲座不多,我们只有快速的 pdf 介绍材料。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多