我在
Java课程中的任务是制作3个三角形.一个左对齐,一个右对齐,一个居中.我必须为什么类型的三角形创建一个菜单,然后输入需要多少行.三角形必须看起来像这样
* ** *** **** * ** *** **** * *** *****
到目前为止,我能够做左对齐的三角形,但我似乎无法得到另外两个.我试过谷歌搜索但没有出现.有人可以帮忙吗?到目前为止我有这个.
import java.util.*; public class Prog673A { public static void leftTriangle() { Scanner input = new Scanner (System.in); System.out.print("How many rows: "); int rows = input.nextInt(); for (int x = 1; x <= rows; x++) { for (int i = 1; i <= x; i++) { System.out.print("*"); } System.out.println(""); } } public static void rightTriangle() { Scanner input = new Scanner (System.in); System.out.print("How many rows: "); int rows = input.nextInt(); for (int x = 1; x <= rows; x++) { for (int i = 1; i >= x; i--) { System.out.print(" "); } System.out.println("*"); } } public static void centerTriangle() { } public static void main (String args []) { Scanner input = new Scanner (System.in); System.out.println("Types of Triangles"); System.out.println("\t1. Left"); System.out.println("\t2. Right"); System.out.println("\t3. Center"); System.out.print("Enter a number: "); int menu = input.nextInt(); if (menu == 1) leftTriangle(); if (menu == 2) rightTriangle(); if (menu == 3) centerTriangle(); } }
样本输出:
Types of Triangles 1. Left 2. Right 3. Center Enter a number (1-3): 3 How many rows?: 6 * *** ***** ******* ********* ***********