【问题标题】:Creating a hollow rectangle in java using scanner and nested for loops使用扫描仪在java中创建一个空心矩形并嵌套for循环
【发布时间】:2020-04-10 02:59:42
【问题描述】:

我非常坚持这一点,我在 Java 中创建了一个程序来制作一个空心矩形,但我得到了一个完整的矩形。分配让我们创建一个名为“矩形”的新 java 类,并在其中编写必要的材料,然后在主代码中调用类和构造函数。我在下面附上了我的代码。我意识到我的 System.out.print 被编码为打印一个空白空间,我这样做是因为当我让它打印我的“drawChar”时,它看起来很离谱。当我寻求帮助时,我只是希望它看起来像一个矩形。

这是我的矩形类:

public class Rectangle {
    private int width;
    private int height;
    private char drawChar;

    public Rectangle(int width,int height, char drawChar)
    {
        this.width = width;
        this.height=height;
        this.drawChar=drawChar;
    }


    public void printOutline()
    {
        for(int i=0; i<height; i++)
        {
            for(int j =0; j<width; j++)
            {
                System.out.print(drawChar);
                if(i==1||i>=height-1||j==0||j==width-1){
                    System.out.print(" ");
                }
                else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

这是主程序:

public class Question1 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a character: ");
        String input = keyboard.nextLine();
        char drawChar = '*';
        if(input.length() > 0 )
            drawChar = input.charAt(0);
        System.out.println("Please enter the width: ");
        int width = Integer.parseInt(keyboard.nextLine());

        System.out.println("Please enter the height: ");
        int height = Integer.parseInt(keyboard.nextLine());

        Rectangle rec = new Rectangle(width,height,drawChar);
        rec.printOutline();

    }
}

这是我的结果:

Enter a character: 
!
Please enter the width: 
5
Please enter the height: 
5
! ! ! ! ! 
! ! ! ! ! 
! ! ! ! ! 
! ! ! ! ! 
! ! ! ! ! 

另外,这里是作业:

 *
 * Write a Rectangle Class with three fields and has a constructor with three parameters,
 *   - integers: width and height
 *   - character: drawChar
 *   Write Setters and Getters (even though you won't use them)
 * The class will have a method called printOutline that prints a rectangle to the
 * console that is the outline based on the dimensions width and height.
 * The method will use the character drawChar as its outline character. The method
 * should use nested for loops to print the output.
 * (HINT: Try to get the output to print a full rectangle without the spaces in the middle,
 * then alter your code to just print the character on the outline - think about your nested for-loops and what
 * values should have a character print or a space print.)
 *
 * The main method will demonstrate this class by asking the user for width, height and
 * a draw character.
 * (HINT: use charAt(0) method to get character from input String)
 * It will call the constructor with these values and then
 * draw a rectangle outline to the console.
 * Input Validation: Do not allow the user to enter negative numbers for height and width.
 * Loop until they enter a positive value.
 *
 * Example Output 1:
 * Please enter a character for your drawing:
 * $
 * Please enter a positive integer width:
 * 4
 * Please enter a positive integer height:
 * 5
 *
 * $$$$
 * $  $
 * $  $
 * $  $
 * $$$$
 *
 * Example Output 2:
 * Please enter a character for your drawing:
 * !
 * Please enter a positive integer width:
 * -1
 * INVALID - enter a positive integer width:
 * 6
 * Please enter a positive integer height:
 * -8
 * INVALID - enter a positive integer height:
 * 10
 *
 * !!!!!!
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !!!!!!
 *

通常我会向我的导师寻求指导,但由于 covid-19 大流行,我们的校园已关闭,教学已推送到在线。我一直在尝试各种解决方案,但无济于事。任何建议将不胜感激。

【问题讨论】:

    标签: java class for-loop java.util.scanner rectangles


    【解决方案1】:

    我首先会通过将问题分解为多个步骤来解决这个问题。您知道您希望第一行打印等于宽度的字符。所以从那开始:使用你的 for 循环迭代地打印字符(并以 println 结束):

    // always start with a complete row
    for (int i = 0; i < width; i ++) {
        System.out.print(drawChar);
    }
        System.out.println();
    

    接下来,您知道要在倒数第二行打印字符,然后是空格,然后是字符:

    // for next set of rows, only print first and last
    for (int j = 1; j < height; j ++) {
        // print the first one
        System.out.print(drawChar);
        // print the spaces
        for (int k = 1; k < width - 1; k ++) {
            System.out.print(" ");
        }
        // print the last one
        System.out.print(drawChar);
        System.out.println();
    }
    

    然后再次打印整行(就像您对第一行所做的那样)。

    要减少冗余代码,您可以开始将其构建到条件中。例如,如果它是第一行或最后一行:

    // print conditionally, depending on row number
    for (int i = 0; i < height; i ++) {
        // print a complete row if it's the first or last row
        if (i == 0 || i == height - 1) {
            for (int j = 0; j < width; j ++) {
                System.out.print(drawChar);
            }
            System.out.println();
        } 
    

    您可以从那里开始构建,以确保准确地表示您的条件。我认为您的条件表达式中混合了行和列逻辑。

    for (int i = 0; i < height; i ++) {
        for (int j = 0; j < width; j ++) {
            // if in the first or last row or first or last column, print character
            if(i == 0 || i == height - 1 || j == 0 || j == width - 1) {
                System.out.print(drawChar);
            } else {
                // otherwise print space
                System.out.print(" ");
            }
            // at end of row, start new line
            if (j == width - 1) {
                System.out.println();
            }
        }
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 2023-03-19
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多