用java程序实现三角形的输出,要求如下

Python09

用java程序实现三角形的输出,要求如下,第1张

class NoParamException extends Exception{

public NoParamException(String message)

{super(message)}

}

class InputDecimalException extends Exception{}

public class HOMEWORK

{

public static float getHeight(String args[])throws NoParamException,InputDecimalException

{

float m

if(args.length==0)

throw new NoParamException("NoParamException occures!")

m=Float.parseFloat(args[0])

if((int)m!=m)throw new InputDecimalException()

return m

}

public static void main(String args[])

{

float H=0

try{

H=getHeight(args)

}

catch(NoParamException e){

System.out.println("NoParamException occures,please input again!")

}

catch(InputDecimalException e){

System.out.println("InputDecimalException occures,please input again!")

}

catch(Exception e){

System.out.println("NoParamException occures,please input again!")

}

for(int i=1i<=Hi++)

    {

        for(int j=0j<H-ij++)

        System.out.print(" ")

          for(int k=0k<2*i-1k++)

              System.out.print("*")

          System.out.print("\n")

    }

}

}

可以进行两种异常控制,一种是无参数异常,一种是输入小数的异常

这是运行过程,记得程序中的publi class名字改过来,与文件名一样

打印杨辉三角代码如下:

public class woo {

public static void triangle(int n) {

int[][] array = new int[n][n]//三角形数组

for(int i=0i<array.lengthi++){

for(int j=0j<=ij++){

if(j==0||j==i){

array[i][j]=1

}else{

array[i][j] = array[i-1][j-1]+array[i-1][j]

}

System.out.print(array[i][j]+"\t")

}

System.out.println()

}

}

public static void main(String args[]) {

triangle(9)

}

}

扩展资料

杨辉三角起源于中国,在欧洲这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年。它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的优美结合。

杨辉三角具有以下性质:

1、最外层的数字始终是1;

2、第二层是自然数列

3、第三层是三角数列;

4、角数列相邻数字相加可得方数数列。