一.作业要求
二.实现效果
1.创建3个类
2.完成学生类
public class Student {
/**
* 学生类
*/
String name;//姓名
int age;//年龄
double score;//成绩
}
3.完成教室类
创建学生对象数组
Student add_student=new Student();
初始化学生方法
- 由于此类有些复杂,我拆分成了两个
- 一个用于添加学生,另一个就是主类
添加学生方法
public void verdict() {
for (int i=0;i<stulist.length;i++){
Student add_student=new Student();
if(stulist[i]==null){
System.out.print("请输入学生年龄:");
add_student.age=ipt.nextInt();
System.out.print("请输入学生姓名:");
add_student.name=ipt.next();
System.out.print("请输入学生成绩:");
add_student.score=ipt.nextDouble();
stulist[i]=add_student;
break;
}
}
}
主类
public void stuLnst(){
for (int i=0;i<stulist.length;i++){
Student add_student=new Student();
switch (i){
case 0:
System.out.println("请输入学生"+(i+1)+"的资料");
verdict();//添加学生
System.out.println(stulist[i].name);
break;
case 1:
System.out.println("请输入学生"+(i+1)+"的资料");
verdict();
System.out.println(stulist[i].name);
break;
case 2:
System.out.println("请输入学生"+(i+1)+"的资料");
verdict();
System.out.println(stulist[i].name);
break;
case 3:
System.out.println("请输入学生"+(i+1)+"的资料");
verdict();
System.out.println(stulist[i].name);
break;
case 4:
System.out.println("请输入学生"+(i+1)+"的资料");
verdict();
System.out.println(stulist[i].name);
break;
default:
break;
}
}
}
排序-按成绩排序
public void Sorting_result(){
double temp = 0;
for (int i = 0; i < stulist.length - 1; i++){//轮数
for (int j = 0; j < stulist.length - i - 1; j++) { //每轮的次数a.Length - i - 1
if (stulist[i+1]==null){
continue;
}
if ( stulist[j].score < stulist[j+1].score){
temp = stulist[j].score;
stulist[j].score = stulist[j+1].score;
stulist[j+1].score = temp;
break;
}
}
}
for (int i=0; i < stulist.length ; i++){
if (stulist[i]==null){
break;
}
System.out.println("第"+(i+1)+"名:"+stulist[i].score);
}
}
排序-按年龄排序
public void Sorting_age(){
int temp = 0;
for (int i = 0; i < stulist.length - 1; i++) {//轮数
for (int j = 0; j < stulist.length - i - 1; j++) { //每轮的次数a.Length - i - 1
if (stulist[i+1]==null){
break;
}
if ( stulist[j].age > stulist[j+1].age){
temp = stulist[j].age;
stulist[j].age = stulist[j+1].age;
stulist[j+1].age = temp;
}
}
}
for (int i=0; i < stulist.length ; i++){
if (stulist[i]==null){
break;
}
System.out.println("第"+(i+1)+"名:"+stulist[i].score);
}
}