public class YH {
public void yangHuiTriangle(int s){ /* System.out.println("请输入行数:"); Scanner sc = new Scanner(System.in); s=sc.nextInt();*/
//创建二维数组 int triangle[][] = new int[]; //遍历二维数组的第一层 for(int i=0;i<triangle.length;i++){ //初始化第二层数组的大小 triangle = new int[i+1];
//打印正三角n行要满足每一行比上一行少一个* int num=triangle.length-i; for(int z=0;z<num;z++){
System.out.print("*");
} //遍历第二层数组 for(int j=0;j<triangle.length;j++){ //两层的数组元素复制为1 if (i==0||j==0||j==triangle.length-1){ triangle[j] = 1;
}else{
//其他数值通过公式计算 triangle[j] = triangle[i-1][j]+triangle[i-1][j-1]; }
System.out.print(triangle[j]+" ");
} System.out.println(); } }
public static void main(String[] args) { YH yh = new YH(); yh.yangHuiTriangle(10); }
}
运行结果如下:
/usr/dev/java/jdk1.8.0_131/bin/java -javaagent:/usr/dev/idea2018/idea-IU-181.5281.24/lib/idea_rt.jar=40129:/usr/dev/idea2018/idea-IU-181.5281.24/bin -Dfile.encoding=UTF-8 -classpath /usr/dev/java/jdk1.8.0_131/jre/lib/charsets.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/deploy.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/cldrdata.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/dnsns.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/jaccess.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/jfxrt.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/localedata.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/nashorn.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/sunec.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/sunjce_provider.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/sunpkcs11.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/ext/zipfs.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/javaws.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/jce.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/jfr.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/jfxswt.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/jsse.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/management-agent.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/plugin.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/resources.jar:/usr/dev/java/jdk1.8.0_131/jre/lib/rt.jar:/home/leyou04/IdeaProjects/testClass/out/production/testClass com.YH **********1 *********1 1 ********1 2 1 *******1 3 3 1 ******1 4 6 4 1 *****1 5 10 10 5 1 ****1 6 15 20 15 6 1 ***1 7 21 35 35 21 7 1 **1 8 28 56 70 56 28 8 1 *1 9 36 84 126 126 84 36 9 1
Process finished with exit code 0
|