參考鏈接: Java程序的輸出 20(繼承)
一、項目名稱 數組實現學生成績統計 二、項目描述 1.項目簡介: 由用戶輸入學生人數與名字,并定義當前學期課程數與課程名,并分別錄入每位學生每門課程成績,系統顯示輸入每位學生每門課程成績,并顯示學生的總分與平均分。 2.程序要求: 1)程序運行時,顯示歡迎信息,并提示輸入班級學生人數,如圖1。??
2)用戶輸入班級人數后,系統提示用戶輸入本學期課程數,如圖2。?
3)輸入學期課程數后,系統提示用戶定義每門課程名, 如圖3、圖4、圖5。?
?
?
4)所有課程名都定義完成后,系統提示輸入學生姓名。如圖6。 5)錄入一個學生姓名后,提示錄入本學期每門課程的成績。當一個學生所有課程錄入完成后,提示錄入下一個學生姓名,及本學期每門課程成績。如圖7,如圖8,如圖9。? ? 6)所有學生姓名與對應的每個學生本學期每門課程成績錄入完成后,系統顯示打印輸入錄入成績,并統計輸入每位學生的課程總分與平均分。如圖10。 三、說明 本程序不涉及文件數據庫操作,僅利用java數組實現。?
代碼實現:?
package cn.jpi.demo;
?
import java.util.Scanner;
?
import org.junit.Test;
?
public class StudentsScoreStatistics {
//? ? 定義全局變量
? ??
//? ? 班級學生名字(數組)
? ? static String[] studentName;
//? ? 學期課程名(數組)
? ? static String[] courseName;
//? ? 學生成績(二維數組)
? ? static int[][] studentScore;
//? ? 學生總分
? ? static int[] studentScoreSum;
//? ? 學生平均分
? ? static double[] studentScoreAvg;
? ??
//? ? 定義Object二維數組來裝矩形
? ? static Object[][] table;
? ??
//? ? 聲明Scanner對象
? ? static Scanner sc = new Scanner(System.in);
? ??
? ? public static void main(String[] args) {
//? ? ? ? 1.歡迎使用
? ? ? ? welcome();
//? ? ? ? 2.輸入班級人數,課程數,課程名以及每位同學的各門課的成績
? ? ? ? print();
//? ? ? ? 3.打印結果
? ? ? ? end();
? ? }
?
//? ? 4.定義輸出總結果方法
? ? private static void end() {
//? ? ? ? 輸出表頭
? ? ? ? System.out.print("學生"+"\t");
//? ? ? ? 遍歷課程名數組
? ? ? ? for(String s:courseName) {
? ? ? ? ? ? System.out.print(s+"\t");
? ? ? ? }
? ? ? ? System.out.println("總分"+"\t"+"平均分");
//? ? ? ? 二維數組的遍歷
? ? ? ? for(int i = 0;i<table.length;i++) {
? ? ? ? ? ? for(int j = 0;j<table[i].length;j++) {
? ? ? ? ? ? ? ? System.out.print(table[i][j]+"\t");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println();
? ? ? ? }
? ? }
? ??
//? ? 2.定義輸入班級人數,課程數,課程名以及錄入每位同學的各門課的成績方法
? ? private static void print() {
//? ? ? ? 輸入班級人數(確定學生數組的長度)
? ? ? ? System.out.println("請輸入班級學生人數:");
? ? ? ? int i? = sc.nextInt();
? ? ? ? studentName = new String[i];
? ??????
//? ? ? ? 確定學生總分數組長度
? ? ? ? studentScoreSum = new int[i];
? ??????
//? ? ? ? 確定學生平均分數組長度
? ? ? ? studentScoreAvg = new double[i];
? ??????
//? ? ? ? 輸入學生課程數(確定課程數組的長度)
? ? ? ? System.out.println("請輸入學期課程數:");
? ? ? ? int j? = sc.nextInt();
? ? ? ? courseName = new String[j];
? ??????
//? ? ? ? 確定學生成績數組長度
? ? ? ? studentScore = new int[i][j];
? ??????
//? ? ? ? 確定矩陣大小
? ? ? ? table = new Object[i][j+3];
? ??????
//? ? ? ? 輸入課程名字(循環)
? ? ? ? for(int k = 0;k<courseName.length;k++) {
? ? ? ? ? ? System.out.println("請定義第"+(k+1)+"門課的名字:");
? ? ? ? ? ? courseName[k] = sc.next();
? ? ? ? }
//? ? ? ? 輸入學生的姓名,并錄入他的每門課的成績(循環嵌套)
//? ? ? ? 聲明總分變量
? ? ? ? int sum = 0;
//? ? ? ? 聲明平均分變量
//? ? ? ? double avg = 0;
? ? ? ? for(int l = 0;l<studentName.length;l++) {
? ? ? ? ? ? System.out.println("請輸入第"+(l+1)+"個學生的姓名:");
? ? ? ? ? ? studentName[l] = sc.next();
? ??????????
? ? ? ? ? ? for(int m = 0;m<courseName.length;m++) {
? ? ? ? ? ? ? ? System.out.println("請輸入"+studentName[l]+"同學"+courseName[m]+"的成績:");
? ? ? ? ? ? ? ? studentScore[l][m] = sc.nextInt();
? ? ? ? ? ? ? ? sum = sum + studentScore[l][m];
//? ? ? ? ? ? ? ? avg = sum/courseName.length;
? ? ? ? ? ? }
? ? ? ? ? ? studentScoreSum[l] = sum;
? ? ? ? ? ? studentScoreAvg[l] = sum/courseName.length;
//? ? ? ? ? ? sum歸0,重新計算
? ? ? ? ? ? sum = 0;
//? ? ? ? ? ? System.out.println(sum+"....."+avg);
? ? ? ? }
//? ? ? ? 為 表格填入數據
? ? ? ? for(int n = 0;n<studentName.length;n++) {
? ? ? ? ? ? for(int o = 0;o<courseName.length+3;o++) {
? ? ? ? ? ? ? ? if(o>=1 && o<=courseName.length) {
? ? ? ? ? ? ? ? ? ? table[n][o] = studentScore[n][o-1];
? ? ? ? ? ? ? ? }else if(o==0) {
? ? ? ? ? ? ? ? ? ? table[n][o] = studentName[n];
? ? ? ? ? ? ? ? }else if(o==courseName.length+1){
? ? ? ? ? ? ? ? ? ? table[n][o] = studentScoreSum[n];
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? table[n][o] = studentScoreAvg[n];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
//? ? 1.定義打印表頭方法
? ? private static void welcome() {
? ? ? ? System.out.println("歡迎使用簡單不簡約成績統計系統^_^");
? ? }
? ? @Test
? ? public void test() {
?
? ? ? ? }
? ? }
}
?
代碼就不細分了(反正也是給我以后自己看的,嘻嘻) 主要講講做這題的過程中遇到的問題吧 我有在兩個地方卡住:?
?
?一個是在給table賦值的時候,我原本想把賦值放在輸入學生成績的那個循環里面,但是要么是索引越界異常,要么是后面的值賦不上去,為null值,此時我就想到它循環的次數和我要給table賦值的長度是不一樣的。 為解決這一問題,我到外面單獨寫一個循環去給table賦值(這個故事告訴我,不該省的代碼還是老老實實的寫)?
?
?
?另外一個是二維數組的問題,我看到題目要求的結果我就很清楚這里是個二維數組,我也是這么做的,但是!我在前面定義的學生成績的數組它是一維的,而且我是在循環里面給他賦值,也就是說我對一個學生的成績賦值完成,給下一個學生的成績賦值,它就會把第一個學生成績的值給覆蓋掉,導致無論我輸出的結果都與最后一個同學的成績一樣。 為解決這一問題,我把學生成績改為二維數組,再將相應的地方改掉就OK了?
?
在測的時候要把(學生數>課程數,學生數=課程數,學生數<課程數)這幾種情況都測一測,因為有一些細節不注意,它可能會存在索引不存在或者索引越界的問題。?
還有一個小作業就不寫上來了,很容易。?
最后:面向對象面向君,不負代碼不負卿。各位加油