繁体   English   中英

3种方法的空心矩形

[英]Hollow rectangle with 3 methods

这是我的代码:

    int width;
    int height;

    put("Enter the width of the rectangle: ");
    width = input.nextInt();

    put("Enter the height of the rectangle: ");
    height = input.nextInt();

    rectangle( height, width );
}

public static void put(String text){
    System.out.print( text );
}

public static void put(String text, int Number){
    int plus;
    for ( plus = Number ; plus >=  1; plus-- ){
        System.out.print( text);
    }
}

public static void rectangle(int height, int width){
    int column;
    int row;

    for ( row = 1; row <= height; row++){
        put("*", width);
        put("\n");
    }
}

我想创建一个空心矩形,我只需要通过rectangle()方法修改

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

应该是这样的^

任何的想法?

编辑一

我找到了一种使用此代码添加第一行和最后一行的方法

public static void rectangle(int height, int width) {
    int column;
    int row;

    for (row = 1; row <= height; row++) {
        if (row == 1 || row == height) {
            put("*", width);
        }
        else {
            put(" ");
        }
        put("\n");
    }
}

我想现在我应该控制宽度,这样我就可以在第一列和最后一列打印两个错过的星星。 我找不到将矩形方法与第二个 put 方法连接起来的方法。

请问有什么想法吗?

尝试从行和列的角度考虑这一点。 在第一行和最后一行( row = 1row = height )中,每列都有一个 * 。 在任何其他行中,第一列和最后一列中有一个* ,其余列中有一个空格。

正如评论中指出的那样,您似乎无条件地将*放在每一列中,但如上所述,您只想对第一行和最后一行执行此操作。

编辑:如果你想在偶数行上包含一个空行,这里有一个提示:“偶数”数的定义是“完全可以被 2 整除”——想想你如何在 Java 中实现它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM