java《學生成績管理系統》功能介紹以及代碼實現

學生成績管理系統

一、程序結構
在這裏插入圖片描述
二、各模塊的功能及程序說明。
關於我的任務部分:定義菜單函數,先打印菜單,再用switch語句給用戶選擇,應用實例化輸入流對象,調用函數完成菜單頁面的打印和選擇;寫一個判斷是否錄入數據的類,通過if語句實現判斷;定義增加函數,通過數組的引用,調用接口實現增加功能;查看即打印功能實現。本軟件採用文件方式寫入,通過對類的封裝來實現格式的整齊,方便查看以及修改,以此提升軟件的清晰性,爲了提升軟件健壯性和可靠性,還使用了throw IOEcxption拋出異常。使用scanner內置的方法next()、nextInt(),接口的直接調用,保證程序的正常運行。
三、源代碼

package class10;

import java.util.Scanner;//導入java輸入流
import java.lang.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Student implements Comparable<Student> {
	private static Student s[] = new Student[1024];
	int n = 0;
	private String name;  // 姓名
	private int num;  // 學號
	private String classAge;  // 班級
	private double math;  // 高等數學
	private double english;  //大學英語
	private double java;  // java
	private double computer;  // 計算機應用基礎
	private double aver; // 平均分
	private double Max;  // 最高分
	private double Min;  // 最低分

	
	// 判斷是否錄入數據
               // throws IOException   這個異常被拋到方法外 在這個方法外被捕獲      拋出異常
               //throws IOException表示此方法有拋出IOException異常的可能性
	public void judge() throws IOException {       
		int i;
		char ch;
		String str;
		Scanner In = new Scanner(System.in);
		if (n == 0) {
			System.out.print("你還沒有錄入任何學生,是否錄入(Y/N):");
			str = In.next();
			ch = str.charAt(0);  // charAt() 方法用於返回指定索引處的字符。索引範圍爲從 0 到 length() - 1。
			while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') {
				System.out.print("輸入有誤,請重新輸入:");
				str = In.next();
				ch = str.charAt(0);
			}
			if (ch == 'Y' || ch == 'y') {
				this.add();    // 增加數據
			}
			if (ch == 'N' || ch == 'n') {
				this.menu();
			}
		}
	}

	
	
	// 定義菜單函數
	public void menu() throws IOException
	{
		int a;// 定義switch語句變量
		Scanner in = new Scanner(System.in);  // 實例化輸入流對象
		System.out.println("*********學生信息管理系統功能表*********");
		System.out.println("*****           1.增加             *****");
		System.out.println("*****           2.查看             *****");
		System.out.println("*****           3.修改             *****");
		System.out.println("*****           4.刪除             *****");
		System.out.println("*****           5.顯示             *****");
		System.out.println("*****           6.排序             *****");
		System.out.println("*****           7.作者             *****");
		System.out.println("*****           0.退出             *****");
		System.out.println("****************************************");
		System.out.print("請選擇(0~7):");
		a = in.nextInt();
		while (a < 0 || a > 7) {
			System.out.print("輸入超出範圍,請重新輸入:");
			a = in.nextInt();
		}
		switch (a) {
		case 1:
			this.add();  // 增加
			break;
		case 5:
			this.show(); // 顯示即打印
			break;
		case 3:
			this.modif();  // 修改信息
			break;
		case 4:
			this.delete();  // 刪除
			break;
		case 2:
			this.look();  // 查看
			break;
		case 6:
			this.SortANDprint();  // 排序
			break;
		case 7:
			this.writer();   //作者
			break;
		case 0:
			System.exit(0);  // 退出
			break;
		}
	}
	
	

	// 定義增加函數
	public void add() throws IOException
	{
		String str, str1, str2;
		int i, num1, t = 1;
		char ch, ch1; 
		FileWriter fw = new FileWriter("C:\\Users\\Lenovo\\Desktop\\C++大法.txt", true);
		fw.write("             錄入的學生信息列表\r\n\r\n學號     姓名     班級     高等數學     大學英語     java課程設計     計算機應用基礎\r\n");
		Scanner In = new Scanner(System.in);
		while (t == 1) {
			System.out.print("請輸入學生學號:");
			num1 = In.nextInt();
			for (i = 0; i < n; i++) {
				while (s[i].num == num1) {
					System.out.println("已存在此學號,請重新輸入");
					System.out.print("請輸入學號:");
					num1 = In.nextInt();
				}
			}
			
			s[n].num = num1;
			str2 = String.valueOf(num1);  // 返回參數的字符串表示形式
			fw.write(str2 + "    ");  // 寫入學號
			System.out.println();
			
			System.out.print("請輸入學生姓名:");
			s[n].name = In.next();
			fw.write(s[n].name + "      ");
			System.out.println();
			
			System.out.print("請輸入學生班級:");
			s[n].classAge = In.next();
			fw.write(s[n].classAge + "      ");
			System.out.println();
			
			System.out.print("請輸入學生高等數學成績:");
			s[n].math = In.nextDouble();
			fw.write(s[n].math + "      ");
			System.out.println();
			
			System.out.print("請輸入學生大學英語成績:");
			s[n].english = In.nextDouble();
			fw.write(s[n].english + "      ");
			System.out.println();
			
			System.out.print("請輸入學生java成績:");
			s[n].java = In.nextDouble();
			fw.write(s[n].java + "      ");
			System.out.println();
			
			System.out.print("請輸入學生計算機應用基礎成績:");
			s[n].computer = In.nextDouble();
			fw.write(s[n].computer + "      ");
			
			s[n].aver = (s[n].math + s[n].english + s[n].java + s[n].computer) / 4;  // 平均分
			fw.write(s[n].aver + "      ");
			
			MaxMin(n);
			fw.write(s[n].Max + "      ");
			fw.write(s[n].Min + "\r\n");	
			++n;
			
			System.out.println();
			System.out.print("是否繼續添加(Y/N)");
			str = In.next();
			ch = str.charAt(0);
			while (ch != 'N' && ch != 'n' && ch != 'Y' && ch != 'y') {
				System.out.print("輸入有誤,請重新輸入:");
				str = In.next();
				ch = str.charAt(0);
			}
			if (ch == 'N' || ch == 'n') {
				fw.close();
				break;
			}
		}
		
		System.out.println();
		System.out.print("是否返回主菜單(Y/N)");
		str1 = In.next();
		ch1 = str1.charAt(0);
		while (ch1 != 'Y' && ch1 != 'y' && ch1 != 'N' && ch1 != 'n') {
			System.out.print("輸入有誤,請重新輸入:");
			str1 = In.next();
			ch1 = str1.charAt(0);
		}
		if (ch1 == 'Y' || ch1 == 'y') {
			this.menu();
		}
		if (ch1 == 'N' || ch1 == 'n') {
			System.out.println("正在退出...謝謝使用!");
			System.exit(0);
		}
	}

	
	
	// 查看即打印功能實現
	public void look() throws IOException {
		int i; 
		this.judge();
		Scanner input = new Scanner(System.in);
		System.out.print("請輸入要查看的學號:");
		i = input.nextInt();
		System.out.println(s[i].toString());

		System.out.println("系統返回主菜單!");
		this.menu();
	}

	
	// 刪除信息功能實現
	public void delete() throws IOException
	{
		this.judge();
		int j = 0, t = 0, k = 0, num1;
		char ch;
		String str;
		Scanner pin = new Scanner(System.in);
		System.out.print("請輸入要刪除的學號:");
		num1 = pin.nextInt();
		for (j = 0; j < n; j++) {
			if (s[j].num == num1) {
				k = 1;  // flag 如果找到 k = 1
				t = j;  // 如果找到將數組下標j給t
			}
		}
		if (k == 0) {
			System.out.println("對不起!你要刪除的學號不存在!");
			System.out.println("系統將返回主菜單!");
			this.menu();
		}
		if (k == 1) {
			System.out.println("你要刪除的學生信息如下:");// 打印管理員要刪除的學生信息
			System.out.println(s[t].toString());
			System.out.println();
			System.out.print("你確定要刪除(Y/N):");
			str = pin.next();
			ch = str.charAt(0);
			while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') {
				System.out.print("輸入有誤,請重新輸入:");
				str = pin.next();
				ch = str.charAt(0);
			}
			if (ch == 'N' || ch == 'n') {
				System.out.println();
				System.out.println("系統返回主菜單!");
				this.menu();
			}
			if (ch == 'Y' || ch == 'y') {
				for (j = t; j < n - 1; j++) {
					s[j] = s[j + 1];  // 刪除的實現,將數組中後面的元素向前移
				}
				n--;
				System.out.println("數據成功刪除!");
				System.out.println("系統返回主菜單!");
				this.menu();
			}
		}
	}
	
	
	
	public void writer() throws IOException {
		System.out.println("張國琛+張澳+文子豪");
	}

	
	
	// 顯示所有數據功能實現
	public void show() throws IOException {  
		FileReader fr = new FileReader("C:\\Users\\Lenovo\\Desktop\\C++大法.txt");
		int a;
		while ((a = fr.read()) != -1) { 
			// read()接口 從輸入流讀取數據的下一個字節,返回的字節的值是一個0~255之間的整數。到達流的末尾返回-1。
			System.out.print((char) a);
		}
		fr.close();
		System.out.println("系統返回主菜單!");
		System.out.println();
		this.menu();
	}

	
	
	// 修改數據信息功能
	public void modif() throws IOException {
		this.judge();
		int j = 0, t = 0, k = 0, num2, num3, moi, c = 1;
		char ch;
		String str, str1, str2;
		Double str3, str4, str5, str6;
		Scanner pin = new Scanner(System.in);
		System.out.print("請輸入要修改的學號:");
		num2 = pin.nextInt();
		for (j = 0; j < n; j++) {
			if (s[j].num == num2) {
				k = 1;
				t = j;
			}
		}
		if (k == 0) {
			System.out.println("對不起!你要修改的學號不存在!");
			System.out.println("系統將返回主菜單!");
			this.menu();
		}
		if (k == 1) {
			System.out.println("你要修改的學生信息如下:");// 打印管理員要刪除的學生信息
			System.out.println(s[t].toString());
			System.out.println();
			System.out.print("你確定要修改(Y/N):");
			str = pin.next();
			ch = str.charAt(0);
			while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') {
				System.out.print("輸入有誤,請重新輸入:");
				str = pin.next();
				ch = str.charAt(0);
			}
			if (ch == 'N' || ch == 'n') {
				System.out.println();
				System.out.println("系統返回主菜單!");
				this.menu();
			}
			while (c == 1) {
				if (ch == 'Y' || ch == 'y') {
					System.out.println("****************************************");
					System.out.println("*****         1.修改學號                     *****");
					System.out.println("*****         2.修改班級                     *****");
					System.out.println("*****         3.修改姓名                     *****");
					System.out.println("*****         4.修改高等數學              *****");
					System.out.println("*****         5.修改大學英語              *****");
					System.out.println("*****         6.修改java          *****");
					System.out.println("*****         7.修改計算機應用基礎   *****");
					System.out.println("****************************************");
					System.out.print("請選擇:");
					moi = pin.nextInt();
					switch (moi) {
					case 1:
						System.out.print("請輸入新的學號:");
						num3 = pin.nextInt();
						s[t].num = num3;
						System.out.println();
						break;
					case 2:
						System.out.print("請輸入新的班級:");
						str1 = pin.next();
						s[t].classAge = str1;
						break;
					case 3:
						System.out.print("請輸入新的姓名:");
						str2 = pin.next();
						s[t].name = str2;
						break;
					case 4:
						System.out.print("請輸入新的高等數學成績:");
						str3 = pin.nextDouble();
						s[t].math = str3;
						s[t].aver = (s[t].math + s[t].english + s[t].java + s[t].computer) / 4;
						MaxMin(t);
						break;
					case 5:
						System.out.print("請輸入新的大學英語成績:");
						str4 = pin.nextDouble();
						s[t].english = str4;
						s[t].aver = (s[t].math + s[t].english + s[t].java + s[t].computer) / 4;
						MaxMin(t);
						break;
					case 6:
						System.out.print("請輸入新的java成績:");
						str5 = pin.nextDouble();
						s[t].java = str5;
						s[t].aver = (s[t].math + s[t].english + s[t].java + s[t].computer) / 4;
						MaxMin(t);
						break;
					case 7:
						System.out.print("請輸入新的計算機應用基礎成績:");
						str6 = pin.nextDouble();
						s[t].computer = str6;
						s[t].aver = (s[t].math + s[t].english + s[t].java + s[t].computer) / 4;
						MaxMin(t);
						break;
					}
					System.out.println("數據已成功修改!");
				}
				System.out.print("是否繼續修改(Y/N)");
				str = pin.next();
				ch = str.charAt(0);
				System.out.println();
				while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') {
					System.out.print("輸入有誤,請重新輸入:");
					str = pin.next();
					ch = str.charAt(0);
				}
				if (ch == 'N' || ch == 'n') {
					break;
				}
			}
		}
		System.out.println();
		System.out.println("系統返回主菜單!");
		this.menu();
	}
	
	
	// 最高分最低分實現
	public void MaxMin(int i) {
		
			s[i].Max = s[i].math;
			if(s[i].english > s[i].math) {
				s[i].Max = s[i].english;
			}
			else if(s[i].java > s[i].math || s[i].java > s[i].english) {
				s[i].Max = s[i].java;
			}
			else if(s[i].computer > s[i].math || s[i].computer > s[i].english || s[i].computer > s[i].java) {
				s[i].Max = s[i].computer;
			}
		
			s[i].Min = s[i].math;
			if(s[i].english < s[i].math) {
				s[i].Min = s[i].english;
			}
			else if(s[i].java < s[i].math || s[i].java < s[i].english) {
				s[i].Min = s[i].java;
			}
			else if(s[i].computer < s[i].math || s[i].computer < s[i].english || s[i].computer < s[i].java) {
				s[i].Min = s[i].computer;
			}					
	}
	
	
	//求平均值
	public String toString() {
		return "Student [num =" + num + ", name =" + name + ", classAge = " 
	+ classAge + ", math = " + math + ", english = " 
	+ english + ", computer = " + computer + ", Java = " 
	+ java + ", aver = " + aver+ '\n' + "]"; 
				
	}


	// 排序方法
	public void SortANDprint() throws IOException {
		
		ArrayList<Student> Students = new ArrayList<>();

		int i = 0;
		for(i = 0; i < n; i++)
		{
			Students.add(s[i]);
		}
		
		Collections.sort(Students);
		FileWriter fw = new FileWriter("C:\\Users\\Lenovo\\Desktop\\C++大法.txt", false);
		for (Student s : Students) {	
			fw.write(s.toString());
			System.out.println(s.toString());
		}
		
		fw.close();
		menu();
	}

	// 重寫
	public int compareTo(Student o) {
		
		return (int)(o.aver - this.aver);
	}
	

	// main 函數
	public static void main(String[] args) throws IOException {
		Student stu = new Student();
		
		for (int i = 0; i < 1024; i++) {
			s[i] = new Student();
		}
		
		stu.menu();
	}
}

四、運行結果演示
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章