- /*
- 需求:封装一个函数,能够打印向上和向下的三角形
- */
- import java.util.Scanner;
- class PrintTriangle
- {
- public static void printTriangle(int a)
- {
- for(int x = 0;x < a; x++)
- {
- for(int y = 0;y <= x; y++)
- {
- System.out.print("*");
- }
- System.out.println();
- }
- System.out.println("-----------------------");
- for(int x = 0;x < a; x++)
- {
- for(int y = x;y < a; y++)
- {
- System.out.print("*");
- }
- System.out.println();
- }
- }
-
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("想打几行的三角,随便打:");
- int a = sc.nextInt();
- printTriangle(a);
- }
- }
复制代码
|
|