我正在尝试创建一个程序,要求用户提供正方形/矩形的宽度和长度尺寸,然后使用#符号将其绘制出来.我几乎了解了,除了我似乎不太了解矩形的右边以正确打印出来…
这是我的代码:
import java.util.Scanner;
public class warmup3
{
public static void main(String[] args)
{
int width;
int length;
Scanner sc= new Scanner(System.in);
System.out.println("How big should the width of the square be?");
width = sc.nextInt();
System.out.println("How big should the length of the square be?");
length= sc.nextInt();
{
for (int y= 0; y < length; y++)
{
for (int x= 0; x < width; x++)
{
if (x == 0 || y == 0)
{
System.out.print("#");
}
else if (x != width && y == length-1)
{
System.out.print("#");
}
else if (y != length && x == width-1)
{
System.out.print("#");
}
else
{
System.out.print("");
}
}
System.out.println("");
}
}
}
}
我知道问题出在第二个else-if语句上,但是我无法解决它…
我无法上传此代码打印出的图片,但基本上是一个几乎完整的矩形,但左侧有两排#,而没有封闭右侧(右侧是打开的)(您应该可以看到为自己).