定义一个StudentList类用来存储Student对象
属性有
1)Student[] list; //list存储学生对象
2)int total; //学生总人数
方法有:
1)StudentList(int length) //length是数组长度
2)boolean add(Student stu) //增加stu到数组中,成功,返回true,否则false
3)boolean remove(int no) //删除第no个数组元素,删除成功,返回true,否则false
4)boolean remove(String number) //删除学号为number的学生,删除成功,返回true,否则false
5)boolean isEmpty() //判断数组是否为空,若是,返回true,否则false
6)Student getItem(int no) //返回第no个学生
7)Student getItem(String number) //返回学号为number的学生,若该生不存在,返回null。
8) int getTotal() 返回学生总人数
编写StudentList类,并且编写一个StudentListTest类,对StudentList类进行测试。
StudentListTest类运行效果:
菜单如下,请输入 1~8代表您要执行的操作:
1.增加1个学生 2.根据学号删除学生 3.根据位置删除学生 4.判断是否为空 5.根据位置返回学生 6.根据学号返回学生 7.输出全部学生信息 8.退出程序
输入样例,每行表示一个操作,第一个元素为操作代码,之后的元素为操作需要用到的数据。具体参看样例。
添加操作,你需要输出当前所有学生信息,格式如下:
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
查询操作,若有该学生,则输出其信息:
Student Info:
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
否则,输出:
No such student
删除操作,若有学生,输出:
Remove successfully
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
若不存在该学生,输出:
No such student
输入样例
1 2011211301 LiHang 88 79 90
1 2011211311 WangZhe 80 79 93
5 10
5 2
3 2
输出样例
#(input)#1 2011211301 LiHang 88 79 90
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
#(input)#1 2011211311 WangZhe 80 79 93
2 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
#(input)#5 10
No such student
#(input)#5 2
Student Info:
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
#(input)#3 2
Remove successfully
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.concurrent.ScheduledExecutorService;
class student{
String student_number;
String student_name;
int math;
int english;
int science;
double average;
DecimalFormat df=new DecimalFormat("#.00");
student(String student_number,String student_name,int math,int english,int science)
{
this.student_number=student_number;
this.student_name=student_name;
this.math=math;
this.english=english;
this.science= science;
average= (math+english+science)/3.0;//1 2011211301 LiHang 88 79 90
}
public String get_out() {
return ("Number:"+this.student_number+"\nName:"+this.student_name+"\nMath:"+this.math+"\nEnglish:"+this.english+"\nScience:"+this.science+"\nAve:"+df.format(average));
}
}
class StudentList{
student[]list;
int total=0;
StudentList(int lenth)
{
list=new student[lenth];
}
boolean add(student stu)
{
if(total>list.length)
{
return false;
}
else
{
list[total]=new student(stu.student_number,stu.student_name,stu.math,stu.english,stu.science);
total++;
return true;
}
}
boolean remove(int no)
{
if(no>total)
{
return false;
}
else {
for(int j=no;j<total-1;j++)
{
list[j]=list[j+1];
}
total--;
return true;
}
}
boolean remove(String number)
{
if(!isEmpty())
{
for(int i=0;i<total;i++)
{
if(list[i].student_number==number)
{
for(int j=i;j<total-1;j++)
{
list[j]=list[j+1];
}
total--;
return true;
}
}
}
return false;
}
boolean isEmpty()
{
if(total==0)return true;
return false;
}
void show()
{
for(int i=0;i<total;i++)
{
System.out.println(list[i].get_out());
}
}
student getitem(int no)
{
if(no>=total)return null;
//no--;
return list[no];
}
student getitem(String number)
{
for(int i=0;i<total;i++)
{
if(list[i].student_number==number)
{
return list[i];
}
}
return null;
}
}
public class StudentListTest {
public static void main(String []args)
{
StudentList list1=new StudentList(100);
String line;
Scanner in =new Scanner( System.in);
while(in.hasNext())//当于是无限循环,检测每次输入
{
line=in.nextLine();
// line=in.nextLine().trim();去掉字符串首尾两边的字符串
String s1[]=line.split(" ");
String s2=s1[0];
int command=Integer.parseInt(s2);
if(command==1)//增加1个学生
{
String student__number =s1[1];
String student__name = s1[2];
int markForMaths =Integer.parseInt(s1[3]);
int markForEnglish=Integer.parseInt(s1[4]);
int markForScience=Integer.parseInt(s1[5]);
student stu=new student(student__number,student__name,markForMaths,markForEnglish,markForScience);
if(list1.add(stu))
{
System.out.println(list1.total+" Students at the moment as described below:");
list1.show();
}
}
else if (command==2)//根据学号删除学生
{
String delete_student_number=s1[1];
if(list1.remove(delete_student_number))
{
System.out.println("Remove successfully");
System.out.println(list1.total+" Students at the moment as described below:");
list1.show();
}
else
{
System.out.println("No such student");
}
}
else if(command==3)//根据位置删除学生
{
int temp=Integer.parseInt(s1[1]);
if(list1.remove(temp-1))
{
//syso
System.out.println("Remove successfully");
System.out.println(list1.total+" Students at the moment as described below:");
list1.show();
}
else {
System.out.println("No such student");//spellchecked inspe
}
}
else if(command==4)//判断是否为空
{
if(list1.isEmpty())
{
System.out.println("为空");
}
else {
{
System.out.println("不为空");
}
}
}
if(command==5)//按照序号
{
int temp=Integer.parseInt(s1[1]);
if(list1.getitem(temp-1)!=null)
{
System.out.println("Student Info:\n"+list1.list[temp-1].get_out());
}
else
{
System.out.println("No such student");
}
}
else if(command==6)//按照学号查找
{
if(list1.getitem(s1[1])!=null)
{
System.out.println("Student Info:\n"+list1.getitem(s1[1]).get_out());
}
else System.out.println("No such student");
}
else if(command==7)
{
list1.show();
}
else if(command==8)
{
System.exit(0);
}
}
}
}