首页 > 代码库 > 数组、二维数组

数组、二维数组

                                                     简单的数组、二位数组
1
package com.lovo; 2 3 import java.util.Scanner; 4 5 /** 6 * 二维数组 7 * 8 * @author 王启文 9 * 10 */11 12 public class D2 {13 public static void main(String[] args) {14 Scanner sc = new Scanner(System.in);15 16 String[] names = { "赵大", "钱二", "孙三", "李四", "周五" };17 String[] course = { "语文", "数学", "英语" };18 double[][] scores = new double[5][3];19 20 for (int i = 0; i < scores.length; ++i) {21 System.out.println("请输入" + names[i] + "的成绩:");22 for (int j = 0; j < scores[i].length; ++j) {23 System.out.print("\t" + course[j] + ":");24 scores[i][j] = sc.nextDouble();25 }26 }27 28 double ChSum = 0;29 double MaSum = 0;30 double EnSum = 0;31 32 for (int i = 0; i < scores.length; ++i) {33 double sum = 0;34 for (int j = 0; j < scores[i].length; ++j) {35 sum += scores[i][j];36 if (j == 0) {37 ChSum += scores[i][j];38 }39 if (j == 1) {40 MaSum += scores[i][j];41 }42 if (j == 2) {43 EnSum += scores[i][j];44 }45 }46 System.out.printf("%s的平均分为:%.2f\n", names[i], sum47 / scores[i].length);48 }49 50 System.out.printf("语文的平均分为:%.2f\n", ChSum / names.length);51 System.out.printf("数学的平均分为:%.2f\n", MaSum / names.length);52 System.out.printf("英语的平均分为:%.2f\n", EnSum / names.length);53 54 sc.close();55 }56 }

杨辉三角

 1 package com.lovo; 2  3 /** 4  * 杨辉三角 5  *  6  * @author 王启文 7  *  8  */ 9 public class D3 {10     public static void main(String[] args) {11         int[][] h = new int[10][];12 13         for (int i = 0; i < h.length; ++i) {14             h[i] = new int[i + 1];15             for (int j = 0; j < h[i].length; ++j) {16                 if (j == 0 || j == i) {17                     h[i][j] = 0;18                 } else {19                     h[i][j] = h[i - 1][j] + h[i - 1][j - 1];20                 }21                 System.out.print(h[i][j] + "\t");22             }23             System.out.println();24         }25     }26 }

 

数组、二维数组