十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
class NoParamException extends Exception{

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供太康網(wǎng)站建設、太康做網(wǎng)站、太康網(wǎng)站設計、太康網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、太康企業(yè)網(wǎng)站模板建站服務,10年太康做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
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");
? ? }
}
}
可以進行兩種異??刂?,一種是無參數(shù)異常,一種是輸入小數(shù)的異常
這是運行過程,記得程序中的publi class名字改過來,與文件名一樣
打印楊輝三角代碼如下:
public class woo {
public static void triangle(int n) {
int[][] array = new int[n][n];//三角形數(shù)組
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年發(fā)現(xiàn)這一規(guī)律的,比楊輝要遲393年。它把二項式系數(shù)圖形化,把組合數(shù)內(nèi)在的一些代數(shù)性質(zhì)直觀地從圖形中體現(xiàn)出來,是一種離散型的數(shù)與形的優(yōu)美結合。
楊輝三角具有以下性質(zhì):
1、最外層的數(shù)字始終是1;
2、第二層是自然數(shù)列;
3、第三層是三角數(shù)列;
4、角數(shù)列相鄰數(shù)字相加可得方數(shù)數(shù)列。
可利用如下代碼輸出:
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();
}
}
}
輸出結果: