java打印杨辉三角
来源:互联网 发布:java的排序函数 编辑:程序博客网 时间:2024/05/17 04:12
package test;
import java.util.Scanner;
/**
* @author: Avery
* @description: 打印杨辉三角
* @date: 2017年7月19日
*/
public class YFTriangle {
public static void printYFTriangle(){
System.out.println("how many lines you want:");
Scanner in = new Scanner(System.in);
int lines = in.nextInt();//输入需要显示的行数
int[] a = new int[lines + 1];
int previous = 1;
for (int i = 1; i <= lines; i ++){
//打印前面空格
for(int k=0;k<lines-i;k++){
System.out.print(" ");
}
//打印三角内空
for (int j = 1; j <= i; j++){
int current = a[j];
a[j] = previous + current;
previous = current;
System.out.printf("%4d ",a[j]);//输出格式控制
}
System.out.println();
}
in.close();
}
public static void main(String[] args) {
printYFTriangle();
}
}
* @author: Avery
* @description: 打印杨辉三角
* @date: 2017年7月19日
*/
public class YFTriangle {
public static void printYFTriangle(){
System.out.println("how many lines you want:");
Scanner in = new Scanner(System.in);
int lines = in.nextInt();//输入需要显示的行数
int[] a = new int[lines + 1];
int previous = 1;
for (int i = 1; i <= lines; i ++){
//打印前面空格
for(int k=0;k<lines-i;k++){
System.out.print(" ");
}
//打印三角内空
for (int j = 1; j <= i; j++){
int current = a[j];
a[j] = previous + current;
previous = current;
System.out.printf("%4d ",a[j]);//输出格式控制
}
System.out.println();
}
in.close();
}
public static void main(String[] args) {
printYFTriangle();
}
}
阅读全文
0 0
- java打印杨辉三角
- java打印杨辉三角
- Java打印杨辉三角
- java打印杨辉三角
- java打印杨辉三角
- java打印杨辉三角
- java打印杨辉三角
- java打印杨辉三角
- Java:打印杨辉三角
- java打印杨辉三角
- java版 打印杨辉三角
- java打印杨辉三角图形
- java语言打印菱形杨辉三角
- Java杨辉三角的打印
- java 实现打印杨辉三角
- Java 中打印杨辉三角
- Java 不用数组打印杨辉三角
- java 下的杨辉三角打印
- Debugger APIs
- android 牛人 汇集
- Yann LeCun:深度学习硬件前瞻
- Activity的生命周期以及两个activity之间跳转的问题
- Java工具类_随机生成任意长度的字符串【密码、验证码】
- java打印杨辉三角
- LeetCode 637 : Average of Levels in Binary Tree(java)
- Ubuntu 14.04 配置tftp服务器
- 设置静态IP
- Android Project和app中两个build.gradle配置的区别
- Debugger Engine API
- 小试牛刀(一)
- OutputStreamWriter分析
- FilterInputStream与装饰模式(2)--BufferedInputStream源码