Java實現(xiàn)簡單班級管理系統(tǒng)
本文設計一個簡單的班級管理系統(tǒng),滿足如下要求:
1、設計學生類Student,包含學號(String型)、姓名(String型)、性別(‘M’/'F’代表男/女)、年齡(int型)、是否黨員(boolean型)、語文(double型)、數(shù)學(double型)等信息,要能夠方便輸出學生信息;
2、設計班級類BanJi,其中創(chuàng)建班級時可指定班級的最大容量。可向班級中批量增加學生信息,以及打印輸出班級中所有學生的信息。
import java.util.Scanner;
class Student{
?? ?String id,name; char sex; int age; boolean isDy; double chinese,math;
?? ?Student(String i,String n,char s,int a,boolean Dy,double c,double m){
?? ??? ?id=i; name=n; sex=s; age=a; isDy=Dy; chinese=c; math=m;
?? ?}
?? ?public String toString(){ //此函數(shù)在用System.out.print(s)打印Student型對象s時,會被自動調用
?? ??? ?String sex1 = "未知";//即此處控制如何輸出學生信息
?? ??? ?if(sex=='M' || sex=='m')
?? ??? ??? ?sex1 = "男";
?? ??? ?else if(sex=='F' || sex=='f')
?? ??? ??? ?sex1 = "女";
?? ??? ?String isDy1 = "未知";
?? ??? ?if(isDy == true)
?? ??? ??? ?isDy1 = "黨員";
?? ??? ?else
?? ??? ??? ?isDy1 = "非黨員";
?? ??? ?return id+" "+name+" "+sex1+" "+age+" "+isDy1+" "+chinese+" "+math;
?? ?}
?? ?void print() {
?? ??? ?System.out.print(this);
?? ?}
}
class BanJi{ //班級類,實際上是存儲學生的順序表,數(shù)組+表長
?? ? Student [] s; // 此處并未創(chuàng)建數(shù)組對象,僅是數(shù)組的引用
?? ? int len;
?? ? BanJi(int x){ s = new Student[x];} //此處創(chuàng)建【數(shù)組】對象,而非Student對象,用[]而不是()!
?? ? void append() { // 向班級中批量增加學生元素
?? ??? ? int i=0;
?? ??? ? String id,name; char sex; int age; boolean isDy; double chinese,math;
?? ??? ? System.out.print("按如下格式輸入:\n");
? ? ? ? ?System.out.print("000 張三 F 20 true 33.44 66\n");
? ? ? ? ?System.out.print("請輸入:\n");
? ? ? ? ?Scanner sc = new Scanner(System.in);
? ? ? ? ?while(sc.hasNext() == true) {
? ? ? ? ?? ? id = sc.next();
? ? ? ? ?? ? name = sc.next();
? ? ? ? ?? ? String xb = sc.next(); sex = xb.charAt(0);// 不能直接讀取char型,xb.charAt(0)取字符串xb的第一個字符
? ? ? ? ?? ? age = sc.nextInt();
? ? ? ? ?? ? isDy = sc.nextBoolean();
? ? ? ? ?? ? chinese = sc.nextDouble();
? ? ? ? ?? ? math = sc.nextDouble();
? ? ? ? ?? ? //必須創(chuàng)建一個Student型對象,并將其填入數(shù)組s
? ? ? ? ?? ? s[i] = new Student(id,name,sex,age,isDy,chinese,math);
? ? ? ? ?? ? i++;
? ? ? ? ?}//end-while 必須輸入結束符才能使hasNext()為假
? ? ? ? ?len = i;
?? ? }
?? ? void print() {
?? ??? ? for(int i=0;i<len;i++)
?? ??? ??? ? System.out.print(s[i]+"\n");
?? ? }
}
class App{
?? ?public static void main(String [] args) {
?? ??? ?BanJi h = new BanJi(50);
?? ??? ?h.append();
?? ??? ?h.print();
?? ?}
}
//本例掌握:
1、系統(tǒng)性地學習了Scanner讀取不同類型數(shù)據(jù)、讀取未知數(shù)量的數(shù)據(jù)(haxNext()–面向String型、hasNextInt()、hasNextDouble())、如何結束輸入(ctrl+Z);
2、重點: Student[] s=new Student[100]; 創(chuàng)建的是一個數(shù)組對象,而非Student對象
注:s是 一個Student[]型 數(shù)組對象,內有100個空指針(即null),
如果希望使用st[i].show(); ,s[i]必須先創(chuàng)建對象,然后才能使用。即:s[i]=new Student(…); //注此處使用圓括號,即調用了Student的構造函數(shù)
否則,將會產生空指針引用異常。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解OpenAPI開發(fā)如何動態(tài)的添加接口實現(xiàn)
這篇文章主要為大家介紹了OpenAPI開發(fā)如何動態(tài)的添加接口實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
將springboot項目生成可依賴的jar并引入到項目中的方法
SpringBoot項目默認打包的是可運行jar包,也可以打包成不可運行的jar包,本文給大家介紹將springboot項目生成可依賴的jar并引入到項目中的方法,感興趣的朋友一起看看吧2023-11-11

