简体   繁体   English

使用扫描仪和嵌套的 for 循环在 java 中创建一个空心矩形

[英]Creating a hollow rectangle in java using scanner and nested for loops

I am very stuck on this, I have created a program in Java to make a hollow rectangle but instead i get a full rectangle.我非常坚持这一点,我在 Java 中创建了一个程序来制作一个空心矩形,但我得到了一个完整的矩形。 The assignment has us create a new java class called 'Rectangle' and code the necessary material in there and then call the class and constructor in the main code.任务让我们创建一个名为“矩形”的新 java class 并在其中编码必要的材料,然后在主代码中调用 class 和构造函数。 I have attached my code below.我在下面附上了我的代码。 I realize that my System.out.print is being coded to print a blank space, i did this because when i had it print my 'drawChar' it looked outrageous.我意识到我的 System.out.print 被编码为打印一个空白空间,我这样做是因为当我让它打印我的“drawChar”时它看起来很离谱。 I just wanted it to look like a rectangle when i was asking for help.当我寻求帮助时,我只是希望它看起来像一个矩形。

This is my Rectangle Class:这是我的矩形 Class:

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

and this is the main program:这是主程序:

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

    }
}

This is my result:这是我的结果:

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

Also, here is the assignment:另外,这里是任务:

 *
 * 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
 *
 * !!!!!!
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !    !
 * !!!!!!
 *

normally i would seek direction from my instructor but due to the covid-19 pandemic our campus is shut down and instruction has been pushed to online.通常我会向我的导师寻求指导,但由于 covid-19 大流行,我们的校园已关闭,教学已被推送到网上。 I have been trying various solutions with no avail.我一直在尝试各种解决方案,但无济于事。 Any suggestions would be greatly appreciated.任何建议将不胜感激。

I'd tackle this first by breaking the problem into steps.我会首先通过将问题分解为步骤来解决这个问题。 You know you want the first row to print characters equal to the width.您知道您希望第一行打印等于宽度的字符。 So start with that: print the character iteratively using your for loop (and finish with a println):所以从那开始:使用你的 for 循环迭代地打印字符(并以 println 结束):

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

Next, you know you want the second through to second last rows to print the character, followed by spaces, followed by the character:接下来,您知道您希望第二行到倒数第二行打印字符,然后是空格,然后是字符:

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

Then finish by printing the full row again (as you did for the first row).然后通过再次打印整行来完成(就像您对第一行所做的那样)。

To cut back the redundant code, you can start to build it into conditions.要减少冗余代码,您可以开始将其构建到条件中。 For example, if it's the first or last row:例如,如果它是第一行或最后一行:

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

You can build up from there to ensure that your conditions are accurately represented.您可以从那里开始构建,以确保准确表示您的条件。 I think you have your row and column logic mixed up in your conditional expression.我认为您的条件表达式中混合了行和列逻辑。

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

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

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