英文:
Creating a hollow rectangle in java using scanner and nested for loops
问题
这是我的矩形类:
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++) {
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
System.out.print(drawChar);
} 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
!!!!!
! !
! !
! !
!!!!!
此外,以下是你的作业:
*
* 编写一个Rectangle类,有三个字段,并带有三个参数的构造函数,
* - 整数:width和height
* - 字符:drawChar
* 编写Setter和Getter方法(即使您不会使用它们)
* 该类将有一个名为printOutline的方法,根据宽度和高度绘制一个矩形的轮廓到控制台。
* 该方法将使用字符drawChar作为其轮廓字符。方法应使用嵌套循环来打印输出。
* (提示:首先尝试在中间不加空格的情况下打印一个完整的矩形,然后修改代码,只打印轮廓上的字符 - 思考嵌套的for循环以及哪些值应该打印字符或空格。)
*
* 主方法将通过询问用户宽度、高度和绘制字符来演示此类。
* (提示:使用charAt(0)方法从输入字符串中获取字符)
* 它将使用这些值调用构造函数,然后在控制台上绘制矩形轮廓。
* 输入验证:不允许用户为高度和宽度输入负数。循环,直到他们输入一个正值。
*
* 示例输出1:
* 请输入绘图字符:
* $
* 请输入正整数宽度:
* 4
* 请输入正整数高度:
* 5
*
* $$$$
* $ $
* $ $
* $ $
* $$$$
*
* 示例输出2:
* 请输入绘图字符:
* !
* 请输入正整数宽度:
* -1
* 无效输入 - 请输入正整数宽度:
* 6
* 请输入正整数高度:
* -8
* 无效输入 - 请输入正整数高度:
* 10
*
* !!!!!!
* ! !
* ! !
* ! !
* ! !
* ! !
* ! !
* ! !
* ! !
* !!!!!!
*
通常情况下,我会从我的讲师那里寻求指导,但由于COVID-19大流行,我们的校园关闭了,教学也转向了在线。我已经尝试了各种解决方案,但都没有成功。非常感谢您提供的任何建议。
英文:
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. 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. 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. I just wanted it to look like a rectangle when i was asking for help.
This is my Rectangle 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. I have been trying various solutions with no avail. Any suggestions would be greatly appreciated.
答案1
得分: 0
我会首先通过将问题分解为几个步骤来解决。首先,你要让第一行打印出与宽度相等的字符。所以从这里开始:使用 for 循环迭代打印字符(最后用 println 完成):
// 总是从完整的一行开始
for (int i = 0; i < width; i++) {
System.out.print(drawChar);
}
System.out.println();
接下来,你要让第二行到倒数第二行打印字符,紧接着是空格,然后再是字符:
// 对于下一组行,只打印第一个和最后一个字符
for (int j = 1; j < height; j++) {
// 打印第一个字符
System.out.print(drawChar);
// 打印空格
for (int k = 1; k < width - 1; k++) {
System.out.print(" ");
}
// 打印最后一个字符
System.out.print(drawChar);
System.out.println();
}
最后,再次打印完整的一行(就像你为第一行所做的那样)。
为了减少重复的代码,你可以开始将其构建为条件。例如,如果是第一行或最后一行:
// 根据行数有条件地打印
for (int i = 0; i < height; i++) {
// 如果是第一行或最后一行,打印完整的一行
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 (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
System.out.print(drawChar);
} else {
// 否则打印空格
System.out.print(" ");
}
// 在行末,开始新的一行
if (j == width - 1) {
System.out.println();
}
}
}
英文:
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):
// 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();
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论