资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java代码输出三角形 java打印三角形代码

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

class NoParamException extends Exception{

成都创新互联专业为企业提供太康网站建设、太康做网站、太康网站设计、太康网站制作等企业网站建设、网页设计与制作、太康企业网站模板建站服务,10年太康做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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=1;i=H;i++)

    {

        for(int j=0;jH-i;j++)

        System.out.print(" ");

      for(int k=0;k2*i-1;k++)

          System.out.print("*");

      System.out.print("\n");

    }

}

}

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

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

Java语言杨辉三角

打印杨辉三角代码如下:

public class woo {

public static void triangle(int n) {

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

for(int i=0;iarray.length;i++){

for(int j=0;j=i;j++){

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、角数列相邻数字相加可得方数数列。

java打印如下数字三角形?

可利用如下代码输出:

package print;

public class Test {

public static void main(String[] args) {

int n = 5;

int num = -1;

for(int i = 1;i n + 1;i++){

System.out.print(i);

if(i == n){

for(int j = 1;j n;j++){

System.out.print("  ");

System.out.print(n + j);

}

}else{

for(int j = i - 1;j 0;j--){

System.out.print(" ");

int end = n * 2 - 1 + n - i;

if(num == -1){

num = end;

}

if(j == 1){

System.out.print(end);

}else{

num = num + 1;

System.out.print(num);

}

}

}

/*换行*/

System.out.println();

}

}

}

输出结果:


当前文章:java代码输出三角形 java打印三角形代码
地址分享:http://cdkjz.cn/article/dohpjci.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220