简单的Java程序,实现学生的数学,语文成绩的录入排序和查询。

不用数据库,用几个比如学生类,排序类,查询类实现学生的数学,语文成绩的录入排序和查询。输入类似这种
stu.add(new Student("张1",95.0f,90.0f)) ;
stu.add(new Student("张2",94.0f,70.0f)) ;
stu.add(new Student("张3",93.0f,54.0f)) ;
stu.add(new Student("张4",92.0f,78.0f)) ;
stu.add(new Student("张5",91.0f,90.0f)) ;
求大神帮我编个程序

我刚写的,可能还有点漏洞

import java.util.*;
import java.util.Scanner;
public class Bd05 {

public static final Scanner s = new Scanner(System.in);

public static void main(String[] args) {
ArrayList<Student> al = new ArrayList<Student>();
home(al);
}

public static void home(List<Student> list){
while(true){
System.out.println("1.录入学生信息     2.查询学生信息     3.删除学生信息     4.退出\n输入数字选择:");
int num = s.nextInt();
switch(num){
case 1:
inputInfo(list);
break;
case 2:
findInfo(list);
break;
case 3:
remove(list);
break;
case 4:
System.exit(0);
}
}
}
public static void findInfo(List<Student> list){ //查询信息
System.out.println("1.升序     2.降序      3.返回\n输入数字选择查询方式(两科的总分):");
int num = 0;
while(true){
num = s.nextInt();
if(num==1){ //升序
Collections.sort(list,new Comparator<Student>(){
public int compare(Student s1, Student s2) {
int n = new Integer(s1.getChinese()+s1.getMath()).compareTo(new Integer(s2.getChinese()+s2.getMath()));
if(n==0)
return s1.getName().compareTo(s2.getName());
return n;
}
});
printAll(list);
}
else if(num==2){ //降序
Collections.sort(list,new Comparator<Student>(){
public int compare(Student s1, Student s2) {
int n = new Integer(s2.getChinese()+s2.getMath()).compareTo(new Integer(s1.getChinese()+s1.getMath()));
if(n==0)
return s2.getName().compareTo(s1.getName());
return n;
}
});
printAll(list);
}
else if(num==3)
break;
else
System.out.println("输入错误,重新输入:");
}
home(list);
}
public static void remove(List<Student> list){ //删除信息
System.out.print("输入需要删除的姓名:");
String name = s.next();
Iterator<Student> it = list.listIterator();
while(it.hasNext()){
Student str = it.next();
if(name.equals(str.getName())){
it.remove();
System.out.println(name+" 删除成功!");
}
}
System.out.println("1.继续     2.返回\n输入数字选择:");
while(true){
int num = s.nextInt();
if(num==1){
remove(list);
}
else if(num==2)
break;
else
System.out.println("输入错误,重新输入:");
}
home(list);
}
public static void printAll(List<Student> list){
int num = 1;
System.out.println("--------------(查询)----------------");
for(Student stu:list){
System.out.println(num++ +".姓名:"+stu.getName()+"|语文成绩:"+stu.getChinese()+"|数学成绩:"+stu.getMath()+"---|总分:"+(stu.getChinese()+stu.getMath()));
}
System.out.println("-----------------------------------");
home(list);
}
public static void inputInfo(List<Student> list){ //输入信息
System.out.println("输入学生姓名:");
String name = s.next();
System.out.println("输入数学成绩:");
int Math = s.nextInt();
System.out.println("输入语文成绩:");
int chinese = s.nextInt();
list.add(new Student(name,Math,chinese));
System.out.println("1.返回     2.继续录入\n输入数字选择:");
while(true){
int num = s.nextInt();
if(num==1)
break;
else if(num==2)
inputInfo(list);
else
System.out.println("输入错误,重新输入:");
}
home(list);
}
}
class Student{
private String name;
private int chinese;
private int math;
Student(String name,int math,int chinese){
this.name = name;
this.chinese = chinese;
this.math = math;
}
public String  getName(){
return name;
}
public int getChinese(){
return chinese;
}
public int getMath(){
return math;
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-15
那就使用List<Student>

输入完,就统计一下
相似回答