Java老师让做的打印星星金字塔

class PrintFunction{
    public void printJinZiTa(int h){
        if(h <= 0){
            System.out.println("错误,可能是未加命令参数或者是参数格式错误,参数应该为大于0整数数字,例如'java myjava 10'");
        }
        int xing = 1;
        int kong  = h - 1;
        for(int i = 1; i <= h; i++){
            for(int j = 1; j<= kong; j++){
                System.out.print(" ");
            }
            kong--;
            for(int k = 1; k <= xing; k++){
                System.out.print("*");
            }
            System.out.print("\n");
            xing = xing + 2;
        }
    }
}
public class MyJava{
    public static void main (String args[]){
        try {
            PrintFunction printFunction = new PrintFunction();
        printFunction.printJinZiTa(Integer.parseInt(args[0]));
        } catch (Exception e) {
            //TODO: handle exception
            System.out.println("错误,可能是未加命令参数或者是参数格式错误,参数应该为大于0整数数字,例如'java myjava 10'!");
        }
    }
}

javac MyJava.java

java MyJava 10

你可能感兴趣的:(java)