package com.user.test;
public class StudentTest02 {
public static void main(String[] args) {
Studet1[] stud = new Studet1[10];
for (int i = 0; i < stud.length; i++) {
stud[i] = new Studet1();
stud[i].number = i + 1;
stud[i].state = (int) (Math.random() * (6 - 1 + 1) + 1);
stud[i].score = (int) (Math.random() * (100 - 0 + 1));
}
StudentTest02 test = new StudentTest02();
test.print(stud);
System.out.println("打印班級為2的信息");
test.searchState(stud, 2);
System.out.println("成績排序并打印邊歷");
test.sort(stud);
test.print(stud);
}
/**
* 遍历数组
* @param stud 被遍历的数组
*/
public void print(Studet1[] stud) {
for (int i = 0; i < stud.length; i++) {
System.out.println(stud[i].info());
}
}
/**
* 查找Studet1数组中的信息
* @param stud 被查找的数组
* @param searchnum 要查找的年纪
*/
public void searchState(Studet1[] stud,int searchnum) {
for (int i = 0; i < stud.length; i++) {
if (stud[i].state == searchnum) {
System.out.println(stud[i].info());
}
}
}
/**
* 對數組進行排序
* @param stud 被排序的數組
*/
public void sort(Studet1[] stud) {
for (int i = 0; i < stud.length; i++) {
for (int j = 0; j < stud.length - 1 - i; j++) {
if (stud[j].score > stud[j + 1].score) {
Studet1 temp = stud[j];
stud[j] = stud[j + 1];
stud[j + 1] = temp;
}
}
}
}
}
class Studet1 {
int number;
int score;
int state;
public String info() {
return "学号:" + number + "\t" + "班級" + state + "\t" + "分數" + score;
}
}
控制台输出:
学号:1 班級4 分數83
学号:2 班級1 分數38
学号:3 班級4 分數25
学号:4 班級5 分數5
学号:5 班級6 分數45
学号:6 班級6 分數21
学号:7 班級4 分數11
学号:8 班級5 分數0
学号:9 班級5 分數27
学号:10 班級6 分數28
打印班級為2的信息
成績排序并打印邊歷
学号:8 班級5 分數0
学号:4 班級5 分數5
学号:7 班級4 分數11
学号:6 班級6 分數21
学号:3 班級4 分數25
学号:9 班級5 分數27
学号:10 班級6 分數28
学号:2 班級1 分數38
学号:5 班級6 分數45
学号:1 班級4 分數83
`