//import java.util.Arrays; //包含Arrays //import java.util.Random; public class HelloWorld { public static void main(String[] args){ // Scanner s = new Scanner(System.in); // System.out.println("请输入一个数字"); // int num = s.nextInt(); YanghuiSanjiao(); //杨辉三角 three_array(); //三维数组 String(); //字符串数组 } public static void YanghuiSanjiao() { //打印杨辉三角的前8行 /* * 1 * 1 1 * 1 2 1 * 1 3 3 1 a[3][1] = a[2][1] + a[20] ====> a[i][j] = a[i-1][j-1] + a[i-1][j] * 1 4 5 4 1 * 1 5 10 10 5 1 */ int [][] a = new int[8][8]; //先初始化为1 for(int i = 0 ; i < a.length ; i++){ for(int j = 0 ; j < a[i].length ; j++){ a[i][j] = 1 ; } } //杨辉三角核心算法 //从第三行开始满足以下规律 for(int i = 2 ; i < a.length ; i++) { for(int j = 1 ; j < i ;j++) { a[i][j] = a[i-1][j-1] + a[i-1][j] ; } } for(int i = 0 ; i < a.length ; i++){ for(int j = 0 ; j <=i ; j++){ System.out.print(a[i][j] + "\t"); } System.out.println(); } } //三维数组 public static void three_array() { int [][][]a = { { {1,2,3}, {1,2,3}, }, { {1,2,3}, {1,2,3}, }, { {1,2,3}, {1,2,3}, }, }; //三维数组的长度就是二维数组的个数 System.out.println(a.length); //a[1]--->三维数组中的第二个二维数组 //ep: //打印的结果是第一个二维数组中的第二个一维数组中的第三个元素的值 System.out.println(a[0][1][2]); } //字符串数组 public static void String() { char a = 'a'; //占用两个字节 //字符串是由多个字符组成的 String name = "YANGYUANXIN" ; //声明一个字符串,用String关键字 //字符串数组 String[] names = {"SY","SAD","ASD","VTE","CH"}; //字符串二维数组,和上面的是一样的,只不过是字符串类型 String[][] teamNames = { {"zhangsan","ls","wangwu"}, {"zhasd","sada","sdasd"}, }; for(int i = 0 ; i < names.length ; i++){ System.out.println(names[i]); } } }