成績查詢類小程式
import java.util.Scanner;
public class array {
public static void main(String[] args) {
int yuWen = 0;
int shuXue = 1;
int waiYu = 2;
int wuLi = 3;
int huaXue = 4;
int shengWu = 5;
//定義總共課程門數,如有需要可以修改為從鍵盤獲取并將科目名字陣列通過鍵盤獲取
int totalScoreCount = 6;
//定義課程名字陣列并賦值
String[] scoreNames = new String[totalScoreCount];
scoreNames[yuWen] = "語文";
scoreNames[shuXue] = "數學";
scoreNames[waiYu] = "外語";
scoreNames[wuLi] = "物理";
scoreNames[huaXue] = "化學";
scoreNames[shengWu] = "生物";
Scanner in = new Scanner(System.in);
System.out.println("請問你要保存幾年的成績?");
int yearToStore = in.nextInt();
//定義分數二維陣列,包含年份和科目資訊
double[][] score = new double[yearToStore][totalScoreCount];
//通過亂數為每年每門課賦值并輸出
for (int i = 0; i < yearToStore; i++) {
for (int j = 0; j < totalScoreCount; j++) {
score[i][j] = Math.random() * 20 + 80;
System.out.println("第" + (i + 1) + "年的" + scoreNames[j] + "成績是:" + score[i][j]);
}
}
//求最大成績和索引
/*
double maxScore = 0;
int maxScoreIdex = -1;
for (int i = 0; i < totalScoreCount; i++) {
if (score[i] > maxScore) {
maxScore = score[i];
maxScoreIdex = i;
}
}
System.out.println("最高成績的科目是:"+scoreNames[maxScoreIdex]+"\t成績為:"+maxScore);
*/
boolean start = true;
while (start) {
int optID;
System.out.println("請選擇要進行的操作:");
System.out.println("1: 求某年最好成績\n" + "2: 求某年的平均成績\n" + "3: 求所有年份最好成績\n" + "4: 求某年某門課的最好成績\n" + "5: 求某門課歷年最好成績");
optID = in.nextInt();
switch (optID) {
case 1:
int seleYear;
double bestScore = 0;
int bestNumScore = 0;
System.out.println("請問你要求那年的最好成績?");
seleYear = in.nextInt();
//輸入檢查
if (seleYear <= 0 || seleYear > yearToStore) {
System.out.println("輸入年份不合法,請輸入[1," + yearToStore + "]之間的年份");
continue;
}
for (int i = 0; i < totalScoreCount; i++) {
if (score[seleYear - 1][i] > bestScore) {
bestScore = score[seleYear][i];
bestNumScore = i;
}
}
System.out.println("第" + seleYear + "年最好成績是:" + scoreNames[bestNumScore] + ",為" + bestScore);
break;
case 2:
seleYear = 0;
double averYearScore;
double totalScore = 0;
System.out.println("請問你想要求那一年的平均成績?");
seleYear = in.nextInt();
//輸入值合法性檢查
if (seleYear <= 0 || seleYear > yearToStore) {
System.out.println("輸入年份不合法,請輸入[1," + yearToStore + "]之間的年份");
continue;
}
//使用foreach回圈替換
/*
for (double x:
score[seleYear]) {
totalScore+=x;
}
*/
for (int i = 0; i < scoreNames.length; i++) {
totalScore += score[seleYear - 1][i];
}
averYearScore = totalScore / scoreNames.length;
System.out.println("第" + seleYear + "年的平均成績是:" + averYearScore);
break;
case 3:
int maxScoreYear = 0;
String maxScoreName = "0";
double maxScore = 0;
for (int i = 0; i < yearToStore; i++) {
for (int j = 0; j < scoreNames.length; j++) {
if (score[i][j] > maxScore) {
maxScore = score[i][j];
maxScoreYear = i + 1;
maxScoreName = scoreNames[j];
}
}
}
System.out.println("第" + maxScoreYear + "年" + maxScoreName + "成績最高,成績為:" + maxScore);
//通過foreach回圈嵌套得出最大分數,但無法得知哪一年和哪門課程,故剔除
/*
for (double[] i:
score) {
for (double j:
i) {
if(j>maxScore){
maxScore = j;
}
}
}
System.out.println(maxScore);
*/
break;
case 4:
maxScoreYear = 0;
maxScore = 0;
maxScoreName = "0";
int seleName;
//將i定義在回圈外面,避免回圈程序中被重置
int y = 1;
System.out.println("請問你要求哪門課程的歷年最好成績?");
for (String x :
scoreNames) {
//按順序輸出課程并在前面加序號,每次執行后累加
System.out.println(y + ":" + x);
y++;
}
seleName = in.nextInt();
//合法性檢查
if (seleName <= 0 || seleName > scoreNames.length) {
System.out.println("輸入課程編號不合法,請輸入[1," + scoreNames.length + "]之間的年份");
continue;
}
for (int i = 0; i < score.length; i++) {
if (score[i][seleName - 1] > maxScore) {
maxScore = score[i][seleName - 1];
maxScoreYear = i + 1;
maxScoreName = scoreNames[seleName - 1];
}
}
System.out.println(maxScoreName + "成績最好的一次是第" + maxScoreYear + "年的" + maxScore);
break;
case 5:
int year;
int num = 1;
System.out.println("請問你要查看第幾年的成績?");
year = in.nextInt() - 1;
//合法性檢查
if (year <= 0 || year > yearToStore) {
System.out.println("輸入年份不合法,請輸入[1," + yearToStore + "]之間的年份");
continue;
}
System.out.println("請問你要查看第幾門課程?");
//同理,得出序號
for (String a : scoreNames) {
System.out.println(num + ":" + a);
num++;
}
int numScore = in.nextInt() - 1;
if (numScore <= 0 || numScore > scoreNames.length) {
System.out.println("輸入課程編號不合法,請輸入[1," + scoreNames.length + "]之間的年份");
continue;
}
System.out.println("第" + (year + 1) + "年的" + scoreNames[numScore] + "成績是:" + score[year][numScore]);
break;
default:
System.out.println("非法數字,程式結束!");
start = false;
//有無作用相同,單純break跳出仍會進入回圈
//break;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/259718.html
標籤:java
