使用scanner和嵌套for循环在java中创建一个空心矩形



我对此非常执着,我用Java创建了一个程序来制作一个空心矩形,但我得到了一个完整的矩形。这个赋值让我们创建一个名为"Rectangle"的新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
*
* !!!!!!
* !    !
* !    !
* !    !
* !    !
* !    !
* !    !
* !    !
* !    !
* !!!!!!
*

通常我会向老师寻求指导,但由于新冠肺炎大流行,我们的校园被关闭,教学被推到了网上。我一直在尝试各种解决方案,但都无济于事。如有任何建议,我们将不胜感激。

我会首先通过将问题分解为多个步骤来解决这个问题。您知道您希望第一行打印与宽度相等的字符。因此,从这个开始:使用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();
}
}
}   

最新更新