绘制一个矩形的边界,中间有空白区域

时间:2014-09-23 19:51:24

标签: java netbeans

我必须使用用户输入的规格绘制矩形的周长 - 它应该看起来像这样

**********
*        *
*        *
*        *
**********
矿山的所有中心全部如何让中心成为白色空间?

到目前为止这是我的代码

消息代码      包lab5;      import java.util.Scanner;

 public class Lab5 {
public static void main(String[] args) {
    Scanner inputReader = new Scanner(System.in);

    int i, j, width, height;
    String x;

    System.out.print("Enter a character: ");
    x = inputReader.nextLine();
    System.out.print("Enter a height: ");
    height = inputReader.nextInt();
    System.out.print("Enter a width: ");
    width = inputReader.nextInt();

    for(i = 1; i <=height; i++) {
        for(j = 1; j <= width; j++) {
            System.out.print(x);
        }
        System.out.println();
    }
}
}

5 个答案:

答案 0 :(得分:2)

将if / else语句压缩到for循环中。如果顶部边框,底部边框,左列或右列,则打印char。

for(i = 1; i <=height; i++) {
    for(j = 1; j <= width; j++) {
        if(i == 1 || i == height || j == 1 || j == width)
            System.out.print(x);
        else
            System.out.print(" ");
    }
    System.out.println();
}

答案 1 :(得分:1)

呃...打印空白。

System.out.print(x);
for(j = 2; j <= width-1; j++) {
    System.out.print(" ");
}
System.out.print(x);

答案 2 :(得分:1)

另一种解决方案无法正常工作

height = inputReader.nextInt();
width = inputReader.nextInt();

它不会为宽度采取正确的输入。

正确的实施方式是:(注意代码已经过试用和编译)

public static void main(String[] args) {
    Scanner inputReader = new Scanner(System.in);
    int i, j, width, height;
    String x;
    System.out.print("Enter a character: ");
    x = inputReader.nextLine();
    System.out.print("Enter a height: ");
    height = Integer.parseInt(inputReader.nextLine());
    System.out.print("Enter a width: ");
    width = Integer.parseInt(inputReader.nextLine());

    for(i = 0; i <= height; i++) {
        System.out.print(x);
        for(j = 1; j <= width-1; j++) {
            if(i==0 || i==height) {
                System.out.print(x);
                continue;
            }
            System.out.print(" ");
        }
        System.out.print(x);
        System.out.println();
    }
}

答案 3 :(得分:0)

当i和j不在边界时,您可以使用System.out.print(&#34;&#34;)打印空格。

import java.util.Scanner;

public class PrintBox {
    public static void main(String[] args) {
        Scanner inputReader = new Scanner(System.in);

        int i, j, width, height;
        String x;

        System.out.print("Enter a character: ");
        x = inputReader.nextLine();
        System.out.print("Enter a height: ");
        height = inputReader.nextInt();
        System.out.print("Enter a width: ");
        width = inputReader.nextInt();

        for(i = 1; i <= height; i++) {
            for(j = 1; j <= width; j++) {
                if (i == 1 || i == height || j == 1 || j == width)
                    System.out.print(x);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

答案 4 :(得分:0)

for(int i=0;i<width;i++){
    System.out.println(x);
}
for(int i=0;i<height;i++){
    System.out.println(x);
    for(int j=0;j<width-2;j++){
       System.out.println(" ");
    }
    System.out.println(x);
}
for(int i=0;i<width;i++){
    System.out.println(x);
}