1 新建Student類,定義學生的屬性
package com.dr.test;
public class Student {
private String name;
private int age;
private int score;
private String department;
private String level;
public int getScore(){
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public String getDepartment(){
return department;
}
public void setDepartment(String department){
this.department=department;
}
public String getLevel(){
return level;
}
public void setLevel(String Level){
this.level=level;
}
}
2 創建StudentTest類,新建學生對象,將學生的成績劃分等級,并輸出
package com.dr.test;
public class StudentTest {
public static void main(String[] args) {
Student[] stuArr=new Student[4];
//########################################
Student student=new Student();
student.setName("宋可");
student.setAge(23);
student.setScore(62);
student.setDepartment("計算機科學系");
stuArr[0]=student;
//#######################################
Student student1=new Student();
student1.setName("田馨");
student1.setAge(25);
student1.setScore(80);
student1.setDepartment("音樂系");
stuArr[1]=student1;
//########################################
Student student2=new Student();
student2.setName("曉楓");
student2.setAge(30);
student2.setScore(78);
student2.setDepartment("美術系");
stuArr[2]=student2;
//########################################
Student student3=new Student();
student3.setName("林嵐");
student3.setAge(25);
student3.setScore(90);
student3.setDepartment("中文系");
stuArr[3]=student3;
for(int j=0;j<stuArr.length;j++){
if(stuArr[j]!=null)
System.out.println(stuArr[j].getName()+" 年齡:"+stuArr[j].getAge()+" 成績:"+stuArr[j].getScore()+" 班級:"+stuArr[j].getDepartment());
}
for(int i=0;i<stuArr.length;i++){
if(stuArr[i]!=null){
int z;
z=stuArr[i].getScore()/10;
switch(z){
case(9):{
System.out.println(stuArr[i].getName()+"等級為A");
break;
}
case(8):{
System.out.println(stuArr[i].getName()+"等級為B");
break;
}
case(7):{
System.out.println(stuArr[i].getName()+"等級為C");
break;
}
case(6):{
System.out.println(stuArr[i].getName()+"等級為D");
break;
}
default:{
System.out.println(stuArr[i].getName()+"等級為E");
break;
}
}
}
}
}
}
謝謝,你的也不錯喲!@Tovep