題目:寫一個學生信息(包括學號,姓名,年齡,年級,成績)管理系統,要求將學生的成績進行處理(90分以上的劃為A等,90到80分的劃為B 等,70到80分的劃為C等,60到70分的劃為D等,60分以下的劃為E等),處理完后將每個學生的基本信息和他的等級一并輸出出來,最后將 A,B,C,D,E五個等級的成績個數輸出出來。
步驟如下:
第一步:創建一個學生類,設置它的一些屬性(學號,姓名,年齡,年級,成績)和方法(設置器set和訪問器get可以用系統添加)。
2.實例化一組學生對象(用一個數組來存儲這一組學生對象),錄入他們的信息(學號,姓名,年齡,年級,成績)(這個通過構造函數創建或者set訪問器設置)。
3.取出他們的成績信息(用get訪問器訪問),然后進行處理,把成績進行分類(A,B,C,D,E五類)。
下面附Java代碼
//構建一個學生類
class Student
{
//定義一個學生類(屬性:學號number,姓名name,年齡age,年級depth,成績mark)
private int number;
private String name;
private int age;
private int depth;
private int mark;
//聲明學生類的構造方法(就是學生類是如何組成的),將五個參數變量分別賦值給五個屬性
public void setNumber(int a)
{
number = a;
}
//學生學號的訪問器
public int getNumber()
{
return number;
}
//學生名字的設置器
public void setName(String b)
{
name = b;
}
//學生名字的訪問器
public String getName()
{
return name;
}
//學生年齡的設置器
public void setAge(int c)
{
age = c;
}
//學生年齡的訪問器
public int getAge()
{
return age;
}
//學生年級的設置器
public void setDepth(int d)
{
depth = d;
}
//學生年級的訪問器
public int getDepth()
{
return depth;
}
//學生成績的設置器
public void setMark(int e)
{
mark = e;
}
//學生成績的訪問器
public int getMark()
{
return mark;
}
}
public class StudentCourse
{
//學生成績主類的主方法,程序就是從這里開始執行的
public static void main(String[] args)
{
//先定義一個學生類數組用于存儲多個學生對象
Student [] student= new Student [10];
//將10個學生對象的基本信息(學號,姓名,年齡,年級,成績)全部實例化出來,可供訪問器get訪問
Student student = new Student();
//添加student1
student.setName("hans");
student.setAge(23);
student.setScore(60);
student.setLable(1);
stuArr[0] = student;
Student student1 = new Student();
//添加student2
student1.setName("rose");
student1.setAge(24);
student1.setScore(90);
student1.setLable(2);
stuArr[1] = student1;
Student student2 = new Student();
//添加student3
student2.setName("good");
student2.setAge(25);
student2.setScore(80);
student2.setLable(3);
stuArr[2] = student2;
Student student3 = new Student();
//添加student4
student3.setName("killva");
student3.setAge(24);
student3.setScore(90);
student3.setLable(2);
stuArr[3] = student3;
Student student4 = new Student();
//添加student5
student4.setName("good1");
student4.setAge(25);
student4.setScore(80);
student4.setLable(3);
stuArr[4] = student4;
Student student5 = new Student();
//添加student6
student5.setName("hans1");
student5.setAge(23);
student5.setScore(60);
student5.setLable(1);
stuArr[5] = student5;
Student student6 = new Student();
//添加student7
student6.setName("rose2");
student6.setAge(24);
student6.setScore(90);
student6.setLable(2);
stuArr[6] = student6;
Student student7 = new Student();
//添加student8
student7.setName("good3");
student7.setAge(25);
student7.setScore(80);
student7.setLable(3);
stuArr[7] = student7;
Student student8 = new Student();
//添加student9
student8.setName("rose4");
student8.setAge(24);
student8.setScore(90);
student8.setLable(2);
stuArr[8] = student8;
Student student9 = new Student();
//添加student5
student9.setName("good5");
student9.setAge(25);
student9.setScore(80);
student9.setLable(3);
stuArr[9] =
student9;
//定義五個整形變量,用于計數A,B,C,D,E五等成績
int A=0;
int B=0;
int C=0;
int D=0;
int E=0;
//定義一個工作變量m,用于表示每個學生對象的成績等級
char m;
//用一個增強型for循環將學生數組中的每一個學生對象遍歷出來
for(Student p : student)
{
//用switch循環實現選擇功能,取出學生的成績,并劃分等級
switch(p.getMark()/10)
{
case 9:{
m='A'; //用一個字符表示成績等級
A++; //等級計數器
break;
}
case 8:{
m='B';
B++;
break;
}
case 7:{
m='C';
C++;
break;
}
case 6:{
m='D';
D++;
break;
}
//當前面所有情況都不滿足時,就執行下面程序
default:{
m='E';
E++;
}
}
//每處理完一次學生對象成績就將該學生對象的基本信息打出來
System.out.println("學號:"+p.getNumber()+" 姓名:"+p.getName()+"
年齡:"+p.getAge()+" 年級:"+p.getDepth()+" 成績:"+p.getMark()+"
等級:"+m);
}
//最后記錄五個等級的成績個數
System.out.println("A等學生個數:"+A);
System.out.println("B等學生個數:"+B);
System.out.println("C等學生個數:"+C);
System.out.println("D等學生個數:"+D);
System.out.println("E等學生個數:"+E);
}
}