public class Demo {
private static int l = 1;
private static String ll = "2";
public static void main(String[] args) {
Student[] stu = new Student[50];
shili(stu);//实例化
insert(stu);//插入信息
sort(stu);//排序
System.out.println("所有学生进出图书馆的信息");
show(stu);//输出排序信息
System.out.println("第三名信息:");
showFirdth(stu);//第三名
System.out.println("第四名和第六名:");
showFourSix(stu);
int count=getSumCount(stu);//成功进出次数
System.out.println("成功进出次数是:"+count);
}
/**
* 初始化
* @param stu
*/
public static void shili(Student[] stu) {
for (int i = 0; i < stu.length; i++) {
stu[i] = new Student();
}
}
/**
* 输入进出记录
* @param stu
*/
public static void insert(Student[] stu) {
int i = 0;
System.out.println("请输入图书馆进出记录");
System.out.println("姓名 学号");
Scanner input = new Scanner(System.in);
for (i = 0; i < stu.length; i++) {
String name = input.next();
String num = input.next();
if(name.equals(".")){
break;
}
if (isExit(num)) {
i = i - 1;
continue;
}
if (isHave(stu, i, num)) {
i = i - 1;
} else {
stu[i].setStuName(name);
stu[i].setStuNum(num);
int c = stu[i].getCount();
stu[i].setCount(c + 1);
}
}
}
/**
* 输出学生数组的信息
* @param stu
*/
public static void show(Student[] stu) {
int i = 0;
System.out.println("姓名\t学号\t次数");
for (i = 0; i < stu.length && stu[i].getStuNum() != null; i++) {
System.out.println(stu[i].getStuName() + "\t" + stu[i].getStuNum()
+ "\t" + stu[i].getCount());
}
}
/**
* 判断之前此学号是否有进出的记录
* @param stu
* @param f
* @param num
* @return
*/
public static boolean isHave(Student[] stu, int f, String num) {
for (int i = 0; i < f; i++) {
if (stu[i].getStuNum().equals(num)) {
int c = stu[i].getCount();
stu[i].setCount(c + 1);
return true;
}
}
return false;
}
/**
* 判断是否能够进入 按学号是 2 3 6的循环判断
* @param num
* @return
*/
public static boolean isExit(String num) {
String n = num.substring(num.length() - 1, num.length());
boolean f = false;
if (n.equals(ll)) {
l++;
f = true;
} else {
return false;
}
switch (l % 3) {
case 1:
ll = "2";
break;
case 2:
ll = "3";
break;
case 3:
ll = "6";
break;
}
return f;
}
/**
* 对信息进行排序
* @param stu
*/
public static void sort(Student[] stu) {
int i = 0, j = 0;
/*按次数排序*/
for (i = 0; i < stu.length - 1 && stu[i].getStuNum() != null; i++) {
for (j = 0; j < stu.length - 1 - i &&stu[j].getStuNum() != null; j++) {
if (stu[j].getCount() < stu[j + 1].getCount()) {
Student t = new Student();
t = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = t;
}
}
}
/*如果次数相同 按学号排序*/
for (i = 0; i < stu.length - 1 && stu[i].getStuNum() != null; i++) {
for (j = 0; j < stu.length - 1 - i &&stu[j].getStuNum() != null; j++) {
if (stu[j].getCount() == stu[j + 1].getCount()) {
if ((stu[j].getStuNum().compareTo(stu[j + 1].getStuNum())) == -1) {
Student t = new Student();
t = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = t;
}
}
}
}
}
/**
* 输出第三名的信息
* @param stu
*/
public static void showFirdth(Student[] stu){
if(stu.length>=3 && stu[2].getStuName()!=null){
String name=stu[2].getStuName();
String num=stu[2].getStuNum();
int count=stu[2].getCount();
System.out.println("\n次数排名为三的"+name+"的进出图书馆的\t次数:"+count+"\t学号:"+num);
}else{
System.out.println("\n进出图书馆的次数没有达到第三名");
}
}
/*
* 输出第四名和第六名的信息
*/
public static void showFourSix(Student[] stu){
if(stu.length>=4 && stu[3].getStuName()!=null){
String name=stu[3].getStuName();
int count=stu[3].getCount();
System.out.println("\n排名为四的"+name+"的进出图书馆的\t次数:"+count);
}else{
System.out.println("\n进出图书馆的次数没有达到第四名");
}
if(stu.length>=6 && stu[5].getStuName()!=null){
String name=stu[5].getStuName();
int count=stu[5].getCount();
System.out.println("\n排名为六的"+name+"的进出图书馆的\t次数:"+count);
}else{
System.out.println("\n进出图书馆的次数没有达到第六名");
}
}
/**
* 获取图书馆的成功次数
*/
public static int getSumCount(Student[] stu){
int sumCount=0;
int i = 0;
for (i = 0; i < stu.length && stu[i].getStuNum() != null; i++) {
sumCount+= stu[i].getCount();
}
return sumCount;
}
}
结果是:
|
-
1.png
(21.12 KB, 下载次数: 0)
-
2.png
(12.07 KB, 下载次数: 0)
-
3.png
(25.08 KB, 下载次数: 0)
|