直角三角形打印
package test1;
import java.util.Scanner;
public class PrintTriangle {
void displayTriangle(int height) {
for (int m = 0; m < height; ++m) {
for (int n = 0; n < m + 1; ++n) {
System.out.print('*');
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个数:");
int s = in.nextInt();
PrintTriangle obj = new PrintTriangle();
obj.displayTriangle(s);
}
}
测试:
请输入一个数:
5
*
**
***
****
*****