我正在为学校做作业,而且我完全不知道如何使用循环。下面是代码:
import java.util.Scanner;
public class DrawHalfArrow {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
System.out.println("Enter arrow base height: " + arrowBaseHeight);
arrowBaseHeight = scnr.nextInt();
System.out.println("Enter arrow base width: " + arrowBaseWidth);
arrowBaseWidth = scnr.nextInt();
System.out.println("Enter arrow head width: " + arrowHeadWidth);
arrowHeadWidth = scnr.nextInt();
// Draw arrow base (height = 3, width = 2)
System.out.println("**");
System.out.println("**");
System.out.println("**");
// Draw arrow head (width = 4)
System.out.println("****");
System.out.println("***");
System.out.println("**");
System.out.println("*");
return;
}
}
我想知道是否有人可以帮助我?我甚至不知道从哪里开始......
基本上我们必须这样做,以便箭头的基础,高度和宽度的用户输入根据用户输入的内容而改变。任何有关如何处理此事的帮助将不胜感激。
答案 0 :(得分:0)
import java.util.Scanner;
public class DrawHalfArrow{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int arrowBaseHeight = 0;
int arrowBaseWidth = 0;
int arrowHeadWidth = 0;
System.out.println("Enter arrow base height: " + arrowBaseHeight);
arrowBaseHeight = scnr.nextInt();
System.out.println("Enter arrow base width: " + arrowBaseWidth);
arrowBaseWidth = scnr.nextInt();
System.out.println("Enter arrow head width: " + arrowHeadWidth);
arrowHeadWidth = scnr.nextInt();
// Draw arrow base (height = 3, width = 2)
for (int i = 0; i < arrowBaseHeight; i++) {
for (int j = 0; j < arrowBaseWidth; j++) {
System.out.print("*");
}
System.out.println();
}
// Draw arrow head (width = 4)
for (int i = 0; i < arrowHeadWidth; i++) {
for (int j = arrowHeadWidth; j>i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}