我正在上我的第一堂Java课,我试图用星号绘制半箭头。我应该使用嵌套循环,其中内部循环绘制星号,外部循环迭代次数等于箭头底部的高度。我已经学会了if-else、while循环和for循环。
到目前为止,当输入以下值时,我已经能够正确地绘制箭头:
箭头底部高度:5
箭头底部宽度:2
箭头头部宽度:4
当我尝试将while循环作为外部循环时,程序会超时。我不知所措。
下一个需要使用的输入是2、3、4。我的代码得到了正确的底部高度(2),但没有正确的底部宽度。
最后一个需要的输入是3、3、7。我的代码完全没有得到正确的结果。这是我到目前为止的代码。
我应该使用什么样的循环来获取正确的宽度?
到目前为止,当输入以下值时,我已经能够正确地绘制箭头:
箭头底部高度:5
箭头底部宽度:2
箭头头部宽度:4
当我尝试将while循环作为外部循环时,程序会超时。我不知所措。
下一个需要使用的输入是2、3、4。我的代码得到了正确的底部高度(2),但没有正确的底部宽度。
最后一个需要的输入是3、3、7。我的代码完全没有得到正确的结果。这是我到目前为止的代码。
我应该使用什么样的循环来获取正确的宽度?
Scanner scnr = new Scanner(System.in);
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
int i = 0;
System.out.println("Enter arrow base height: ");
arrowBaseHeight = scnr.nextInt();
System.out.println("Enter arrow base width: ");
arrowBaseWidth = scnr.nextInt();
System.out.println("Enter arrow head width: ");
arrowHeadWidth = scnr.nextInt();
for (i = 1; i <= arrowBaseHeight; ++i) {
// Draw arrow base (height = 3, width = 2)
System.out.println("**");
}
// Draw arrow head (width = 4)
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
输出箭头的示例:
**
**
**
**
****
***
**
*