1.JAVA 利用ArrayList实现个学生信息存储的功能

1.JAVA 利用ArrayList实现个学生信息存储的功能


建立one类

package 练手;
import java.util.*;
public class one {
	static long number=20200000;
	public static void main(String [] args) {
		System.out.println("输入将要储存的学生信息!");
		ArrayList <student> list=new ArrayList<>();
		int cloose;
		while(true) {
			System.out.println("0.XXXXX1.新建学生信息2.删除学生信息3.修改学生信息4.输出学生信息5.退出\n输入操作1~4数字:");
			cloose=student.cheakIntAndIn();
			if(cloose<0||cloose>5) {
				System.out.println("别乱输入!输入1~4数字呀");
				continue;
			}
			if(cloose==5) {
				System.out.println("谢谢使用!");
				break;
				}
				
			switch(cloose) {
			case(0):
				System.out.println("随机新建100个学生信息");
				createStudent.create(list);
				break;
			case(1):
				student stu=new student();
			    stu.setname();
			    stu.setsex();
			    stu.numeber=number;
			    list.add(stu);
			    number++;
			    System.out.println("信息存储成功!");
				break;
			case(2):
				RemoveStudent(list);
				break;
			case(3):
				Recompose(list);
				break;
			case(4):
				int i=1;
				System.out.println("学号\t\t姓名\t性别");
				Iterator iterator=list.iterator();
				while(iterator.hasNext()) {
					System.out.println(iterator.next());
				}
				break;
			}
		}
	
	}
	//删除
	public static void RemoveStudent(ArrayList list) {
		System.out.println("输入您想要找的学生的学号!");
		long number1;
		number1=student.cheakLongAndIn();
		Iterator it=list.iterator();
		while(it.hasNext()) {
			student stu2 =(student) it.next();
			if(stu2.numeber==number1) {
				System.out.println("该学生信息为:"+stu2.numeber+"\t"+stu2.name+"\t"+stu2.sex);
				it.remove();
				System.out.println("该学生信息已删除!");
				break;
			}
			if(!it.hasNext()) {
				System.out.println("该学号不存在!");
			}
		}
	}
	//修改
	public static void Recompose(ArrayList list) {
		System.out.println("输入您想要找的学生的学号!");
		long number1;
		number1=student.cheakLongAndIn();
		Iterator it=list.iterator();
		while(it.hasNext()) {
			student stu2=(student)it.next();
			int cloose;
			if(stu2.numeber==number1) {
				System.out.println("该学生信息为:"+stu2.numeber+"\t"+stu2.name+"\t"+stu2.sex);
				System.out.println("改变同学姓名是否,1.是,2.否");
				while(true) {
				
				cloose=student.cheakIntAndIn();
				if(cloose==1||cloose==2)
					break;
				System.out.println("输入数字1~2");
				}
				if(cloose==1)
				     stu2.setname();
				System.out.println("改变同学性别是否,1.是,2.否");
				while(true) {
				
				cloose=student.cheakIntAndIn();
				if(cloose==1||cloose==2)
					break;
				System.out.println("输入数字1~2");
				}
				if(cloose==1)
				   stu2.setsex();
				System.out.println("信息更改成功");
				break;
			}
			
		}
		if(!it.hasNext()) {
			System.out.println("该学号不存在!");
		}
	}
	
}

建立student类

package 练手;
import java.util.*;
public class student {
	Scanner in=new Scanner(System.in);
    long numeber;
	String name;
	String sex;
	public void setname() {
		System.out.println("输入姓名!");
		boolean a=true;
		while(a) {
			try {
				Scanner in=new Scanner(System.in);
				this.name=in.next();
				a=false;
			}catch(Exception e) {
				System.out.println("输入信息有错误!");
				a=true;
			}
		}
		
	}
	//性别选择
	public void setsex(){
		System.out.println("选择性别:1.男2.女");
		
		while(true) {
		  int cloose=cheakIntAndIn();
		  if(cloose!=1&&cloose!=2) {
			  System.out.println("输入有误,请输入数字1和2");
			  continue;
		  }
		  if(cloose==1) {
			  this.sex="男";
			  break;
			  }
		  else {
			  this.sex="女";
			  break;
			  }
		}
		
	}
	//输入变量,并且检查输入的信息是否是整型变量
	public static int cheakIntAndIn()
	{   int a;
		while(true) {
			try {
				Scanner in=new Scanner(System.in);
				a=in.nextInt();
				break;
			}catch(Exception e){
				System.out.println("输入的信息有错误!");
			}
		}
		return a;
	}
	//输入变量,并且检查输入的信息是否为长整型
	public static long cheakLongAndIn()
	{   long a;
		while(true) {
			try {
				Scanner in=new Scanner(System.in);
				a=in.nextLong();
				break;
			}catch(Exception e){
				System.out.println("输入的信息有错误!");
			}
		}
		return a;
	}
	public String toString() {
		return numeber+"\t"+name+"\t"+sex;
	}
}

createStudent 类 用于随机生成学生信息用于测试

package 练手;
import java.util.*;
public class createStudent {
	static void create(ArrayList list){
		int i;
		for(i=0;i<100;i++,one.number++) {
			student a=new student();
			a.name=RandomName();
			a.sex=RandomSex();
			a.numeber=one.number;
			list.add(a);
		}
	}
	static String RandomName(){
		String [] one= {"张","李","王","刘","周","廖","戴","肖","胡","孙"};
		String [] two= {"成","晓","小","和","琴","涛","欢","清","建","存"};
		int i;
		int longName;//名字长度
		String name="";
		Random rd=new Random();
		longName=rd.nextInt(1)+2;
		for(i=0;i<longName;i++) {
			if(i==0)
				name=name+one[rd.nextInt(9)];
			name=name+two[rd.nextInt(9)];
		}
		return name;
	}
	static String RandomSex() {
		Random rd=new Random();
		String sex="";
		if(rd.nextBoolean())
			sex="女";
		else
			sex="男";
		return sex;
	}
}

加油奥利给!

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值