您的位置 首页 java

JAVA 二维数组,例程

import java.util.Scanner;

public class CreatArray {

public static void main(String[] args) {

/* 初始化

int book[][] = new int[3][4];

int[][] book1 =new int[3][1];

int[] book2[] =new int[1][2];

int[][] book3 = new int[][] {{1,2,3},{7,8,9}};

System.out.println(book3[0][2]);*/

/*多维数组存储多年成绩*/

//声明六个变量,分别代表6门科目;

int yuWenIndex = 0;

int shuXueIndex = 1;

int waiYuIndex = 2;

int wuLiIndex = 3;

int huaXueIndex = 4;

int shengWuIndex = 5;

//每门课的成绩

String names[] = new String[6];

names[yuWenIndex] = “语文”;

names[shuXueIndex] = “数学”;

names[waiYuIndex] = “外语”;

names[wuLiIndex] = “物理”;

names[huaXueIndex] = “化学”;

names[shengWuIndex] = “生物”;

Scanner in = new Scanner(System.in);

System.out.println(“请问要保存几年的成绩?”);

int yearStore = in.nextInt();

double scores[][] = new double[yearStore][names.length];

for (int i = 0; i < yearStore; i++) {

for (int j = 0; j < names.length; j++) {

scores[i][j] = 80 + java.lang.Math.random() * 20;

System.out.println(“第” + (i + 1) + “年” + “的” + names[j] + “的成绩是:” + scores[i][j]);

}

}

boolean conti=true;

while (conti) {

System.out.println(“请输入要进行的操作编号!”);

System.out.println(“1:求某年最好的成绩\n” + “2:求某年的平均成绩\n”

+ “3:求所有年份最好的成绩\n” + “4:求某门课历年最好的成绩”);

int oprId = in.nextInt();

switch (oprId) {

case 1:

System.out.println(“请输入要求哪一年的最好成绩”);

int year = in.nextInt() – 1;

if (year < 0 || year >= yearStore) {

System.out.println(“非法的年份”);

break;

}

int bestYearofScocre = 0;

for (int i = 0; i < scores[year].length; i++) {

if (scores[year][bestYearofScocre] < scores[year][i]) {

bestYearofScocre = i;

}

}

System.out.println(“第” + (year + 1) + “年最好的成绩是:” + names[bestYearofScocre] + “:” + scores[year][bestYearofScocre]);

break;

case 2:

System.out.println(“请输入要求哪一年的平均成绩”);

year = in.nextInt() – 1;

if (year < 0 || year >= yearStore) {

System.out.println(“非法的年份”);

break;

}

double totalScore = 0;

for (int i = 0; i < scores[year].length; i++) {

totalScore += scores[year][i];

}

System.out.println(“第” + (year + 1) + “年的平均成绩是:” + (totalScore / names.length));

break;

case 3:

int bestofScoreyearId = 0;

year = 0;

for (int i = 0; i < scores.length; i++) {

//System.out.println(“scores.length is ” + scores.length);

for (int j = 0; j < scores[i].length; j++) {

// System.out.println(“scores[i].length is ” + scores[i].length);

if (scores[year][bestofScoreyearId] < scores[i][j]) {

year = i;

bestofScoreyearId = j;

}

}

}

System.out.println(“历年最好成绩为第:” + (year + 1) + “年的” + names[bestofScoreyearId] + “成绩:” + scores[year][bestofScoreyearId]);

break;

case 4:

System.out.println(“请问要查询哪门课历年最好的成绩!”);

int scoreId = in.nextInt()-1;

year=0;

for (int i = 1; i < scores.length; i++) {

if (scores[year][scoreId]<scores[i][scoreId]){

year=i;

}

}

System.out.println(names[scoreId]+”的历年最好成绩是:”+”第”+(year+1)+”年,成绩为:”+scores[year][scoreId]);

break;

default:

System.out.println(“程序结束!”);

conti =false;

}

}

}

}

文章来源:智云一二三科技

文章标题:JAVA 二维数组,例程

文章地址:https://www.zhihuclub.com/180044.shtml

关于作者: 智云科技

热门文章

网站地图