绘制一个矩形的周长,在中间有空白



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

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

我的所有中心都满了——我怎么才能让中心变成空白?

这是我到目前为止的代码

源代码包装lab5;导入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();
    }
}
}

将if/else语句压缩到for循环中。打印上边框、下边框、左列或右列的字符。

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

嗯。。。打印空白。

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

其他解决方案无法工作,因为我们无法对进行处理

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

当i和j不在边界时,可以使用System.out.print(")打印空间。

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

相关内容

  • 没有找到相关文章

最新更新