java空心正方形代码_从Java中的用户输入绘制空心星号正方形/矩...
我正在嘗試創建一個程序,要求用戶提供正方形/矩形的寬度和長度尺寸,然后使用#符號將其繪制出來.我幾乎了解了,除了我似乎不太了解矩形的右邊以正確打印出來…
這是我的代碼:
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語句上,但是我無法解決它…
我無法上傳此代碼打印出的圖片,但基本上是一個幾乎完整的矩形,但左側有兩排#,而沒有封閉右側(右側是打開的)(您應該可以看到為自己).
總結
以上是生活随笔為你收集整理的java空心正方形代码_从Java中的用户输入绘制空心星号正方形/矩...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: $nextTick 源码
- 下一篇: robocode_Robocode策略