【发布时间】:2015-03-12 04:15:42
【问题描述】:
这是家庭作业。但是,我已经完成了几乎所有的代码,并且只漏掉了一部分。该程序应该根据用户指定的高度和宽度值打印出一个矩形。矩形需要是空心的,但事实并非如此。我是 Java 新手,所以我不确定在我的代码中实现什么来使矩形空心。任何帮助,将不胜感激。谢谢! 例子。
高度 =4 宽度 =8
我得到了什么
********
********
********
********
我需要什么
********
* *
* *
********
下面是我的代码。感谢您的帮助!
import java.util.Scanner;
public class Rectangle
{
public static void main(String[] args)
{
int height;
int width;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the height of the rectangle.");
height = keyboard.nextInt();
if (height < 0)
{
height = 1;
}
System.out.println("Please enter the width of the rectangle.");
width = keyboard.nextInt();
if(width < 0)
{
width = 1;
}
for(int h = 0; h < height; h++)
{
for(int w = 0; w < width; w++)
{
System.out.print("*");
}
System.out.println(" ");
}
}
}
【问题讨论】:
-
在打印 *.条件应检查 h 和 w 是否为 0 或 max(height-1 或 width-1)。给出比这更多的提示需要我把修改后的代码放在这里。我相信你不会想要它。