创建5个学生对象,并赋给一个学生数组,每个学生有以下属性:学号、姓名、年龄,请按顺序实现以下任务:
子任务1:将学生按学号排序输出。
子任务2:给所有学生年龄加1。
子任务3:在实现子任务2的基础上,统计大于20岁的学生人数。
package fouth;
import java.util.Comparator;
public class three {
public static void main(String[] args) {
// TODO Auto-generated method stub
students [] s = new students[5];
s[0] = new students("1","张三",20);
s[1] = new students("3","李四",18);
s[2] = new students("4","王五",19);
s[3] = new students("2","赵六",21);
s[4] = new students("5","陈七",19);
int count = 0;
for(int j = 0;j<s.length-1;j++) {
for(int k = j+1;k<s.length;k++) {
if(s[j].id.compareTo(s[k].id)>0) {
students t;
t = s[j];
s[j] = s[k];
s[k] = t;
}
}
}
for(int x = 0;x<s.length;x++) {
System.out.println("学生排序为:学号"+s[x].id +"姓名:"+s[x].name+"年龄:"+ s[x].age);
}
for(int i = 0;i<s.length;i++) {
if(s[i].age >20) {
count++; //统计大于20岁的学生人数
}
s[i].age = s[i].age+1; //给所有学生年龄加1
System.out.println("所有学生年龄加1:学号"+s[i].id +"姓名:"+s[i].name+"年龄:"+ s[i].age);
}
System.out.println("大于20岁的学生人数:"+count);
}
}
class students{
String id; //学号
String name; //名字
int age; //年龄
public students() {}
public students(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
}
路是自己的,没必要拿别人的标准衡量自己,它只配成为垫脚石。