java的环境变量如何设置?- -| 回首页 | 2005年索引 | - -百鸡问题的程序代码

个人编写的一个关于杨辉三角的代码

                                      

import java.io.*;
public class Yanghuisanjiao{
 public static void main(String[] args) throws IOException{
  int n;
  String s;
  BufferedReader bu;
  try{
   System.out.print("请输入行数:");
   bu=new BufferedReader(new InputStreamReader(System.in));
   s=bu.readLine();
  }
  catch(IOException e){
   System.out.println("不是数字型");
   return;
  }
  try{
   n=Integer.parseInt(s);
   }
   catch(NumberFormatException p){
    System.out.println("输入的为非数值");
    return;
   }

   if(n<0)
   throw new IOException("输入值不能为负数!");
 
 
  int yhsj[][]=new int[n][n];
  for(int i=0;i   for(int j=0;j<=i;j++){
    
    yhsj[i][0]=1;
    if(i==j){
     yhsj[i][j]=1;
    }
    if(i>1&&j>0)
    yhsj[i][j]=yhsj[i-1][j]+yhsj[i-1][j-1];
   }
  }
  System.out.println("杨辉三角为:");
        for(int i=0;i        for(int j=0;j<=i;j++)
          System.out.print(yhsj[i][j]+"  ");
          
          System.out.println();
         }
 }
}

【作者: laodabest】【访问统计:】【2005年06月16日 星期四 13:31】【 加入博采】【打印

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=1941608

回复

验证码:   
评论内容: