在Java中打印矩形

问题描述 投票:0回答:3
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);
    }
}

我正在尝试打印具有输入高度和宽度的矩形,但是它们显示了不同的图案。有什么办法可以嵌套它们?

java rectangles
3个回答
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

1
投票

执行以下操作:

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

0
投票
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快速介绍材料。

© www.soinside.com 2019 - 2024. All rights reserved.