【发布时间】: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