目录
1 飞机票
2 生成一个五位数验证码
3 数字加密
4 数字解密
5 抢红包
6 双色球系统
7 用户登录
8 金额转换
9 手机号屏蔽
10 罗马数字转换
11 调整字符串
12 初级学生管理系统(学生数据的管理)
13 学生管理系统(用户的相关操作)
14 学生管理系统合体
学生信息类
用户类
1 飞机票
代码实现:
import java.util.Scanner;
public class F1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("请输入票价: ");double jia = input.nextDouble();System.out.print("请输入月份: ");int yue = input.nextInt();System.out.print("请输入你所坐飞机舱(经济舱或头等舱) ");String can = input.next();methodjia(jia,yue,can);}private static void methodjia(double jia, int yue, String can) {if (yue>=5 && yue<=10){if (can.equals("经济舱")){jia=jia*0.85;}else if(can.equals("头等舱")) {jia = jia * 0.9;}elseSystem.out.println("输入错误");return;} else if((yue>10 && yue<=12) || (yue<5 && yue>0)){if (can.equals("头等舱")){jia=jia*0.7;}else if(can.equals("经济舱")){jia=jia*0.65;}elseSystem.out.println("输入错误");return;} else if(yue<0||yue>12){System.out.println("错误");return;}System.out.println(jia);}
}
2 生成一个五位数验证码
代码实现:
import java.util.Random;public class Yzm {public static void main(String[] args) {char []arr = new char[52];Random rand = new Random();for (int i = 0; i < 26; i++) {arr[i] = (char) (65+i);}for (int i = 0; i <26; i++) {arr[i+26] = (char) (97+i);}String s ="";for (int i = 0; i < 4; i++) {char k = arr[rand.nextInt(52)];s=s+k;}int i = rand.nextInt(10);s=s+i;System.out.println(s);}
}
3 数字加密
代码实现:
import java.util.Scanner;public class Jm {public static void main(String[] args) {int num = new Scanner(System.in).nextInt();//输入一个数组jiami(intToArray3(num));//方法调用}private static void jiami(int[] arr) {//加密方法每个位数+5,每个位数对10取模,再倒序for (int i = 0; i < arr.length; i++) {arr[i]+=5;}for (int i = 0; i < arr.length; i++) {arr[i]%=10;}for (int i = 0,j = arr.length-1; i < j; i++,j--) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}int num = 0;for (int i = 0; i < arr.length; i++) {num= num*10+arr[i];}System.out.println(num);}private static int[] intToArray3(int num){int len = String.valueOf(num).length();//得到这个数的长度以便创建数组int[] resultArray = new int[len];//创建数组while (num != 0){//给数组一一赋值resultArray[len-1] = num % 10;num = num / 10;len--;}return resultArray;//返回位数组成的数组}
}
4 数字解密
import java.util.*;public class Jiemi {public static void main(String[] args) {int num = new Scanner(System.in).nextInt();jie(int01(num));}private static void jie(int[] arr){for (int i = 0,j=arr.length-1; i < j; i++,j--) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}for (int i = 0; i < arr.length; i++) {if (arr[i]+10<15){arr[i] = arr[i]+10;}}
// 4 3 2 1 0 9 8 7 6 5
// 14 13 12 11 10 9 8 7 6 5
// 9 8 7 6 5 4 3 2 1 0for (int i = 0; i < arr.length; i++) {arr[i]-=5;}int shuchu = 0;for (int i = 0; i < arr.length; i++) {shuchu= shuchu*10+arr[i];}System.out.println(shuchu);}private static int[] int01(int num){int len = String.valueOf(num).length();int[] arr = new int[len];while (num!=0){arr[len-1] = num%10;num/=10;len--;}return arr;}
}
5 抢红包
User类创建
public class User {private String name;private int money;public User(){}public User(String name,int money){this.name=name;this.money=money;}public void show(){System.out.println("Name:"+name+" Money:"+money);}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}
}
Manage类
import java.util.ArrayList;public class Manager extends User{public Manager(String name, int money) {super(name, money);}public Manager() {super();}
//需要一个集合用来存储发红包的个数以及各个金额public ArrayList<Integer> List(int totalmoney,int count){//发的钱ArrayList<Integer> list = new ArrayList<>();int leftmoney = super.getMoney();//群主总共的钱if(leftmoney < totalmoney) return list;//群主没那么多钱int averagemoney = totalmoney/count;//平均每个人int modemoney = totalmoney%count;//除不尽的余数加在list最后一个for(int i = 0 ; i < count-1 ; i++){list.add(averagemoney);}super.setMoney(leftmoney - totalmoney);//群主剩的钱list.add(modemoney+averagemoney);return list;//返回红包集合}
}
Member类
import java.util.ArrayList;
import java.util.Random;public class Member extends User{public Member() {super();}public Member(String name, int money) {super(name, money);}public void receive(ArrayList<Integer> list){int random = new Random().nextInt(list.size());//随即索引int num = list.remove(random);//将这个数去除,并且取出int money = super.getMoney()+num;//把钱加上super.setMoney(money);//更新前的金额}
}
调用
import java.util.ArrayList;
import java.util.Scanner;public class Using {public static void main(String[] args) {Member A= new Member("贤哥",0);Member B = new Member("超哥",0);Member C = new Member("小哥",0);Manager qun = new Manager("群主",10000);System.out.println("原来的钱");qun.show();A.show();B.show();C.show();System.out.println("================");System.out.println("你想发多少");int F = new Scanner(System.in).nextInt();ArrayList<Integer> list = qun.List(F,3);A.receive(list);B.receive(list);C.receive(list);qun.show();A.show();B.show();C.show();}
}
运行结果:
6 双色球系统
代码实现:
package itheima;import java.util.Scanner;
import java.util.Random;public class seven {public static void main(String[] args) {int [] arr = creatarr();int [] arr1 = maicaipiao();jiage(find(arr,arr1),zuihouyige(arr,arr1));}private static void jiage(int num,int lannum){int hongnum = num-lannum;if (hongnum>=0 && lannum==1 &&hongnum<3){System.out.println("恭喜中奖5元");}else if ((hongnum==4&&lannum==0)||(hongnum==3 && lannum==1)){System.out.println("恭喜中奖10元");}else if ((hongnum==4&&lannum==1)||(hongnum==5 && lannum==0)){System.out.println("恭喜中奖200元");} else if(hongnum==5 && lannum==1){System.out.println("恭喜中奖3000元");}else if(hongnum==6 && lannum==0){System.out.println("恭喜中奖500 0000元");} else if (hongnum==6 && lannum==1) {System.out.println("恭喜中奖1000 0000元");}elseSystem.out.println("一分钱没中");}private static int zuihouyige(int [] arr1,int [] arr2){int lannum=0;if (arr1[6]==arr2[6]){lannum=1;}return lannum;}private static int[] maicaipiao(){Scanner P = new Scanner(System.in);//int[] arr = new int[]{P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt(),P.nextInt()};int[] arr = new int[7];for (int i = 0; i < 6;) {System.out.println("输入第"+(i+1)+"个数:");int num = P.nextInt();if (num >0 && num<34) {if (isPrime(arr,num)){arr[i] = num;i++;}elseSystem.out.println("已经存在");}elseSystem.out.println("输入范围错误");}System.out.println("输入第7个数:");while(true){int num7= P.nextInt();if (num7>0 && num7<17) {arr[6] = num7;for (int i : arr) {System.out.print(i+" ");}System.out.println(" ");return arr;}elseSystem.out.println("范围错误");}}private static int[] creatarr() {int[] arr = new int[7];//七色球Random rand = new Random();for (int i = 0; i < arr.length-1;) {int suinum = rand.nextInt(33)+1;//前六个数1-33if (isPrime(arr, suinum)) {arr[i] = suinum;i++;}}arr[arr.length-1] = rand.nextInt(16)+1;//第七个数1-16return arr;}private static boolean isPrime(int []arr,int n) {//是不是已经出现没有返回truefor (int i : arr) {if (n==i){return false;}}return true;}private static int find(int[] arr1, int[] arr2) {//多少个相同的int i = 0, j = 0;int count = 0;while (i < arr1.length && j < arr2.length) {if (arr1[i] == arr2[j]) {count++;i++; // 移动 arr1 的指针j++; // 移动 arr2 的指针} else if (arr1[i] < arr2[j]) {i++;} else {j++;}}return count;}
}
7 用户登录
代码实现
用户名三次机会出错退出系统。
用户名正确进入系统输入密码,密码三次错误锁定用户。
import java.util.Scanner;public class User {public static void main(String[] args) {String Username = "anxian";String UserPassword = "123456";Scanner sc = new Scanner(System.in);String username = null;int num = 0;while (num < 3) {System.out.println("输入你的用户名:");username = sc.nextLine();if (username.equals(Username)) {break;}else {num++;System.out.println("用户名错误,你还有"+(3-num)+"次机会");}}if (username.equals(Username)) {for (int i = 0; i < 3; i++) {System.out.println("输入你的密码:");String password = sc.nextLine();if (password.equals(UserPassword)) {System.out.println("登陆成功");break;} else {System.out.println("信息错误还有" + (2 - i) + "次机会");if (2-i == 0){System.out.println("用户"+Username+"被锁定,请联系客服。");}}}}}
}
8 金额转换
import java.util.Scanner;public class abc {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n;while (true){System.out.println("enter a number");n = sc.nextInt();if ( n>0 && n<9999999 ){break;}elseSystem.out.println("输入错误");}String str="";do {int g = n % 10;n = n / 10;str = daxie(g).concat(str);//变成大写} while (n != 0);int len = str.length();for (int i = 0; i < (7-len); i++) {//把前置零加上String s = "零";str = s.concat(str);}
//方法一
// char []arr = str.toCharArray();//变成字符数组
// String last = "";
// last +=arr[0]+"百";
// last+=arr[1]+"拾";
// last+=arr[2]+"万";
// last+=arr[3]+"千";
// last+=arr[4]+"百";
// last+=arr[5]+"拾";
// last+=arr[6]+"元";
// System.out.println(last);//方法二String []arrs = {"百","拾","万","千","百","拾","元"};for (int i = 0; i < str.length(); i++) {char ch = str.charAt(i);System.out.print(ch);System.out.print(arrs[i]);}}private static String daxie(int n){String []arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};return arr[n];}
}
9 手机号屏蔽
import java.util.Scanner;public class abc {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String str = sc.nextLine();String str1 = str.substring(0, 3);String str2 = str.substring(str.length()-4);System.out.println(str1+"****"+str2);}
}
10 罗马数字转换
代码实现
import java.util.Scanner;public class abc {public static void main(String[] args) {System.out.println("输入一个数字");Scanner sc = new Scanner(System.in);String str;while (true) {str = sc.next();boolean flag = Isintnum(str);if (flag) {break;}elseSystem.out.println("请重新输入");}StringBuilder p = new StringBuilder();for (char c : str.toCharArray()) {int i = c - '0';p.append(Luoma(i));}System.out.println(p);}private static String Luoma(int num){String [] arr = {"","I ","II ","III ","IV ","V ","VI ","VII ","VIII ","IX "};return arr[num];}private static boolean Isintnum(String str){for (int i = 0; i < str.length(); i++) {if (str.charAt(i) < '0' || str.charAt(i) > '9') {return false;}}return true;}
}
11 调整字符串
代码实现
import java.util.Scanner;public class abc {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String next = sc.next();String next2 = sc.next();boolean flag = false;for (int i = 0; i < next.length(); i++) {next = xuanzhuan(next);if (next2.equals(next)) {System.out.println("YES");flag = true;}}if (!flag) {System.out.println("NO");}}private static String xuanzhuan(String next){String s;s = next.charAt(0)+"";next = next.substring(1);next = next+s;return next;}
}
12 初级学生管理系统(学生数据的管理)
创建一个学生类
public class Student {private String id;private String name;private int age;private String address;public Student() {}public Student(String id, String name, int age,String address) {this.id = id;this.name = name;this.age = age;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
相关方法的实现
import java.util.ArrayList;
import java.util.Scanner;public class StudentManage {public static void main(String[] args) {ArrayList<Student> students = new ArrayList<>();loop:while (true) {System.out.println(" ==欢迎来到学生管理系统==");System.out.println(" 输入 1 - 添加学生信息");System.out.println(" 输入 2 - 删除学生信息");System.out.println(" 输入 3 - 更改学生信息");System.out.println(" 输入 4 - 查询学生信息");System.out.println(" 输入 5 - 退出系统");System.out.println(" ");System.out.print("输入你的需求: ");Scanner sc = new Scanner(System.in);int choice = sc.nextInt();switch (choice) {case 1: addstudent(students);break;case 2: delstudent(students);break;case 3: changestudent(students);break;case 4: squrestudent(students);break;case 5: {System.out.println("已退出学生管理系统");break loop;} default:System.out.println("没有这个选项,请重新输入:");}}}private static void squrestudent(ArrayList<Student> students) {//查询遍历,(空学生表的判断)if (students.isEmpty()) {System.out.println("请添加后再查询");}else{System.out.println("id 姓名 年龄 住址");for (Student student : students) {System.out.println(student.getId() + " " + student.getName()+ " " + student.getAge() + " " + student.getAddress());}}}private static void changestudent(ArrayList<Student> students) {//更改学生表的成员,根据id信息更改if (students.isEmpty()) {System.out.println("请添加后再更改");}else{Scanner sc = new Scanner(System.in);System.out.println("输入id");String id = sc.next();if(!idtest(students, id)){System.out.println("id 匹配成功");System.out.println("输入姓名");String name = sc.next();System.out.println("输入年龄");int age = sc.nextInt();System.out.println("输入地址");String address = sc.next();for (Student student : students) {if (student.getId().equals(id)) {student.setId(id);student.setName(name);student.setAge(age);student.setAddress(address);System.out.println("更改成功");}}}elseSystem.out.println("无这个 id 信息,请重新选择你的需求");}}private static void delstudent(ArrayList<Student> students) {//删除学生表成员信息,要判断是否存在System.out.println("请输入id信息");Scanner sc = new Scanner(System.in);String id = sc.next();for (int i = 0; i < students.size(); i++) {if (students.get(i).getId().equals(id)) {students.remove(i);System.out.println("删除成功");break;}if ( i == students.size() - 1) {System.out.println("无这个学生信息,请添加之后再进行操作");}}}private static void addstudent(ArrayList<Student> students) {//添加学生表成员信息// System.out.println("分别输入 id 姓名 年龄 住址");
// Scanner sc = new Scanner(System.in);
// Student student = new Student(sc.next(),sc.next(),sc.nextInt(),sc.next());//添加方法一
// students.add(student);Student student = new Student();Scanner sc = new Scanner(System.in);//添加方法二System.out.println("输入id");while(true){String id = sc.next();if (idtest(students,id)) {student.setId(id);break;}elseSystem.out.println("id已经存在 请重新输入");}System.out.println("输入姓名");String name = sc.next();student.setName(name);System.out.println("输入年龄");int age = sc.nextInt();student.setAge(age);System.out.println("输入住址");String address = sc.next();student.setAddress(address);students.add(student);System.out.println("学生信息添加成功");}private static boolean idtest(ArrayList<Student> students,String id) { // 不存在返回truefor (Student student : students) {if (student.getId().equals(id)) {return false;}}return true;}
}
13 学生管理系统(用户的相关操作)
三种需求
用户账号注册
用户账号登陆
用户忘记密码
代码实现
1 用户类
public class UserPro {private String USerName;private String Password;private String phoneNum;private String personId;public UserPro() {}public UserPro(String USerName, String Password, String phoneNum, String personId) {this.USerName = USerName;this.Password = Password;this.phoneNum = phoneNum;this.personId = personId;}public String getUSerName() {return USerName;}public void setUSerName(String USerName) {this.USerName = USerName;}public String getPassword() {return Password;}public void setPassword(String password) {Password = password;}public String getPhoneNum() {return phoneNum;}public void setPhoneNum(String phoneNum) {this.phoneNum = phoneNum;}public String getPersonId() {return personId;}public void setPersonId(String personId) {this.personId = personId;}
}
2 登录的具体模块实现
import java.util.*;public class StuProMax {public static void main(String[] args) {ArrayList<UserPro> userPros = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请输入你的需求: 1 ->登录 2-> 注册 3-> 找回密码 4-> 退出系统");int a = sc.nextInt();switch (a) {case 1:dengLu(userPros);break;case 2:zhuCe(userPros);break;case 3:zhaoHui(userPros);break;case 4:System.out.println("感谢使用");System.exit(0);default:System.out.println("没有这个选项");}}}private static void zhuCe(ArrayList<UserPro> userPros) {//新建对象UserPro P = new UserPro();//首先用户名输入Scanner sc = new Scanner(System.in);//用于判断用户名是否添加成功boolean yongHu = false;//给定三次机会创建for (int i = 0; i < 3; i++) {System.out.println("请输入你要创建的用户名");String name = sc.next();//用户名的规范性问题//用于统计大小写数字个数int count = 0, count1 = 0, count2 = 0;//判断是否存在用户名(唯一性)boolean equals = false;for (UserPro userPro : userPros) {equals = userPro.getUSerName().equals(name);}if (!equals) {//长度问题if (name.length() >= 3 && name.length() <= 15) {for (char c : name.toCharArray()) {//需要为大小写加数字if (c >= 'A' && c <= 'Z') {count += 1;} else if (c >= 'a' && c <= 'z') {count1 += 1;} else if (c >= '0' && c <= '9') {count2 += 1;} else {System.out.println("非法符号,你还有"+(2-i)+"次机会");break;}}} elseSystem.out.println("用户名长度错误,你还有"+(2-i)+"次机会");}if (count > 0 && count1 > 0 && count2 > 0) {P.setUSerName(name);System.out.println("用户名注册成功");yongHu = true;break;} elseSystem.out.println("需要存在大写字母小写字母数字,你还有"+(2-i)+"次机会");}//密码输入//判断密码输入是否完成boolean passWordFirsts = false;loop:if (yongHu) {while (true) {//两次密码需要相同System.out.println("请输入密码:");String passWordFirst = sc.next();System.out.println("再次确认密码:");String passWordSecond = sc.next();if (passWordFirst.equals(passWordSecond) && passWordFirst.length() >= 6) {P.setPassword(passWordFirst);System.out.println("密码设置成功");passWordFirsts = true;break loop;} elseSystem.out.println("两次设置的密码不相同或者密码位数未达到六位,请重新输入");}}//身份证号的输入boolean personIds = false;loop:if (passWordFirsts){while (true){System.out.println("请输入你的身份证号");String personId = sc.next();if (personId.length()==18) {if (personId.charAt(0) != '0'){String personId2 = personId.substring(0,personId.length()-1);int countNum = 0;if ((personId.charAt(17) >= '0'&& personId.charAt(17) <= '9') || personId.charAt(17) =='x'|| personId.charAt(17) =='X'){for (char c : personId2.toCharArray()) {if (c >= '0' && c <= '9') {countNum += 1;if (countNum == 17){P.setPersonId(personId);System.out.println("身份证验证成功");personIds = true;break loop;}}}}elseSystem.out.println("最后一位数必须是数字或者x,X");}elseSystem.out.println("首位数需要不为0");}elseSystem.out.println("长度错误");}}//手机号的输入boolean phoneNums = false;loop:if (personIds){while (true){System.out.println("请输入你的手机号:");String phoneNum = sc.next();int countNums = 0;if (phoneNum.length() == 11 && phoneNum.charAt(0) == '1') {for (char c : phoneNum.toCharArray()) {if (c >= '0' && c <= '9') {countNums += 1;if (countNums == 11){P.setPhoneNum(phoneNum);System.out.println("电话号码验证成功");phoneNums = true;break loop;}}}}elseSystem.out.println("手机号位数输入错误,或者首位数字不为1");}}if (phoneNums){userPros.add(P);System.out.println("恭喜你注册成功");}else{System.out.println("注册失败");System.out.println();}}private static void zhaoHui (ArrayList<UserPro> userPros) {Scanner input = new Scanner(System.in);System.out.println("输入你的账户名");//判断账户名是否存在boolean yongHu = false;String yongHuName = input.next();for (UserPro i : userPros) {if (i.getUSerName().equals(yongHuName)) {yongHu = true;System.out.println("请输入你的手机号码");String phoneNum = input.next();if (phoneNum.equals(i.getPhoneNum())) {System.out.println("请输入你的身份证号码");String personID = input.next();if (personID.equals(i.getPersonId())) {while (true) {//两次密码需要相同System.out.println("请输入新密码:");String passWordFirst = input.next();System.out.println("再次确认密码:");String passWordSecond = input.next();if (passWordFirst.equals(passWordSecond) && passWordFirst.length() >= 6) {i.setPassword(passWordFirst);System.out.println("密码设置成功");break;} elseSystem.out.println("两次设置的密码不相同,或者密码位数未达到6位,请重新输入");}}elseSystem.out.println("身份证号码验证失败");}elseSystem.out.println("手机号码验证失败");}if (!yongHu) {System.out.println("账户未存在");}}}private static void dengLu(ArrayList<UserPro> userPros) {Scanner input = new Scanner(System.in);System.out.println("输入你的账户名");//判断账户名是否输入成功boolean yongHu = false;String userDeng = input.next();for (UserPro i: userPros){if (i.getUSerName().equals(userDeng)) {yongHu = true;System.out.println("账号输入正确");for (int j = 0; j < 3;) {System.out.println("请输入你的密码:");String userMiMa = input.next();if (userMiMa.equals(i.getPassword())) {j+=3;System.out.println("密码正确");for (int k = 0; k < 3;) {System.out.println("这是一段验证码");String yanZeng = yanZengMa();System.out.println(yanZeng);System.out.println("请输入验证码确认");String next = input.next();if (next.equals(yanZeng)) {System.out.println("验证码输入正确,登陆成功");k+=3;}else{k++;System.out.println("验证码输入错误你还有"+(3-k)+"次机会");}}} else {j++;System.out.println("密码错误你还有"+(3-j)+"次机会");}}}}if (!yongHu){System.out.println("用户尚未注册或者你的密码多次错误或者验证码多次出错,请重试");}}private static String yanZengMa(){//验证码生成程序char []arr = new char[52];Random rand = new Random();for (int i = 0; i < 26; i++) {arr[i] = (char) (65+i);}for (int i = 0; i <26; i++) {arr[i+26] = (char) (97+i);}StringBuilder s = new StringBuilder();for (int i = 0; i < 4; i++) {char k = arr[rand.nextInt(52)];s.append(k);}//4个大小写1个数字int i = rand.nextInt(10);s.append(i);return s.toString();}}
14 学生管理系统合体
学生信息类
public class Student {private String id;private String name;private int age;private String address;public Student() {}public Student(String id, String name, int age,String address) {this.id = id;this.name = name;this.age = age;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
用户类
public class UserPro {private String USerName;private String Password;private String phoneNum;private String personId;public UserPro() {}public UserPro(String USerName, String Password, String phoneNum, String personId) {this.USerName = USerName;this.Password = Password;this.phoneNum = phoneNum;this.personId = personId;}public String getUSerName() {return USerName;}public void setUSerName(String USerName) {this.USerName = USerName;}public String getPassword() {return Password;}public void setPassword(String password) {Password = password;}public String getPhoneNum() {return phoneNum;}public void setPhoneNum(String phoneNum) {this.phoneNum = phoneNum;}public String getPersonId() {return personId;}public void setPersonId(String personId) {this.personId = personId;}
}
管理系统
import java.util.*;public class StuProMax {public static void main(String[] args) {ArrayList<UserPro> userPros = new ArrayList<>();Scanner sc = new Scanner(System.in);while (true) {System.out.println("欢迎来到学生管理系统");System.out.println("请输入你的需求: 1 ->登录 2-> 注册 3-> 找回密码 4-> 退出系统");int a = sc.nextInt();switch (a) {case 1:dengLu(userPros);break;case 2:zhuCe(userPros);break;case 3:zhaoHui(userPros);break;case 4:System.out.println("感谢使用");System.exit(0);default:System.out.println("没有这个选项");}}}private static void zhuCe(ArrayList<UserPro> userPros) {//新建对象UserPro P = new UserPro();//首先用户名输入Scanner sc = new Scanner(System.in);//用于判断用户名是否添加成功boolean yongHu = false;//给定三次机会创建for (int i = 0; i < 3; i++) {System.out.println("请输入你要创建的用户名");String name = sc.next();//用户名的规范性问题//用于统计大小写数字个数int count = 0, count1 = 0, count2 = 0;//判断是否存在用户名(唯一性)boolean equals = false;for (UserPro userPro : userPros) {equals = userPro.getUSerName().equals(name);}if (!equals) {//长度问题if (name.length() >= 3 && name.length() <= 15) {for (char c : name.toCharArray()) {//需要为大小写加数字if (c >= 'A' && c <= 'Z') {count += 1;} else if (c >= 'a' && c <= 'z') {count1 += 1;} else if (c >= '0' && c <= '9') {count2 += 1;} else {System.out.println("非法符号,你还有"+(2-i)+"次机会");break;}}} elseSystem.out.println("用户名长度错误,你还有"+(2-i)+"次机会");}if (count > 0 && count1 > 0 && count2 > 0) {P.setUSerName(name);System.out.println("用户名注册成功");yongHu = true;break;} elseSystem.out.println("需要存在大写字母小写字母数字,你还有"+(2-i)+"次机会");}//密码输入//判断密码输入是否完成boolean passWordFirsts = false;loop:if (yongHu) {while (true) {//两次密码需要相同System.out.println("请输入密码:");String passWordFirst = sc.next();System.out.println("再次确认密码:");String passWordSecond = sc.next();if (passWordFirst.equals(passWordSecond) && passWordFirst.length() >= 6) {P.setPassword(passWordFirst);System.out.println("密码设置成功");passWordFirsts = true;break loop;} elseSystem.out.println("两次设置的密码不相同或者密码位数未达到六位,请重新输入");}}//身份证号的输入boolean personIds = false;loop:if (passWordFirsts){while (true){System.out.println("请输入你的身份证号");String personId = sc.next();if (personId.length()==18) {if (personId.charAt(0) != '0'){String personId2 = personId.substring(0,personId.length()-1);int countNum = 0;if ((personId.charAt(17) >= '0'&& personId.charAt(17) <= '9') || personId.charAt(17) =='x'|| personId.charAt(17) =='X'){for (char c : personId2.toCharArray()) {if (c >= '0' && c <= '9') {countNum += 1;if (countNum == 17){P.setPersonId(personId);System.out.println("身份证验证成功");personIds = true;break loop;}}}}elseSystem.out.println("最后一位数必须是数字或者x,X");}elseSystem.out.println("首位数需要不为0");}elseSystem.out.println("长度错误");}}//手机号的输入boolean phoneNums = false;loop:if (personIds){while (true){System.out.println("请输入你的手机号:");String phoneNum = sc.next();int countNums = 0;if (phoneNum.length() == 11 && phoneNum.charAt(0) == '1') {for (char c : phoneNum.toCharArray()) {if (c >= '0' && c <= '9') {countNums += 1;if (countNums == 11){P.setPhoneNum(phoneNum);System.out.println("电话号码验证成功");phoneNums = true;break loop;}}}}elseSystem.out.println("手机号位数输入错误,或者首位数字不为1");}}if (phoneNums){userPros.add(P);System.out.println("恭喜你注册成功");}else{System.out.println("注册失败");System.out.println();}}private static void zhaoHui (ArrayList<UserPro> userPros) {Scanner input = new Scanner(System.in);System.out.println("输入你的账户名");//判断账户名是否存在boolean yongHu = false;String yongHuName = input.next();for (UserPro i : userPros) {if (i.getUSerName().equals(yongHuName)) {yongHu = true;System.out.println("请输入你的手机号码");String phoneNum = input.next();if (phoneNum.equals(i.getPhoneNum())) {System.out.println("请输入你的身份证号码");String personID = input.next();if (personID.equals(i.getPersonId())) {while (true) {//两次密码需要相同System.out.println("请输入新密码:");String passWordFirst = input.next();System.out.println("再次确认密码:");String passWordSecond = input.next();if (passWordFirst.equals(passWordSecond) && passWordFirst.length() >= 6) {i.setPassword(passWordFirst);System.out.println("密码设置成功");break;} elseSystem.out.println("两次设置的密码不相同,或者密码位数未达到6位,请重新输入");}}elseSystem.out.println("身份证号码验证失败");}elseSystem.out.println("手机号码验证失败");}if (!yongHu) {System.out.println("账户未存在");}}}private static void dengLu(ArrayList<UserPro> userPros) {Scanner input = new Scanner(System.in);System.out.println("输入你的账户名");//判断账户名是否输入成功boolean yongHu = false;String userDeng = input.next();for (UserPro i: userPros){if (i.getUSerName().equals(userDeng)) {yongHu = true;System.out.println("账号输入正确");for (int j = 0; j < 3;) {System.out.println("请输入你的密码:");String userMiMa = input.next();if (userMiMa.equals(i.getPassword())) {j+=3;System.out.println("密码正确");for (int k = 0; k < 3;) {System.out.println("这是一段验证码");String yanZeng = yanZengMa();System.out.println(yanZeng);System.out.println("请输入验证码确认");String next = input.next();if (next.equals(yanZeng)) {System.out.println("验证码输入正确,登陆成功");k+=3;ArrayList<Student> students = new ArrayList<>();loop:while (true) {System.out.println(" ==欢迎来到学生管理系统==");System.out.println(" 输入 1 - 添加学生信息");System.out.println(" 输入 2 - 删除学生信息");System.out.println(" 输入 3 - 更改学生信息");System.out.println(" 输入 4 - 查询学生信息");System.out.println(" 输入 5 - 退出系统");System.out.println(" ");System.out.print("输入你的需求: ");Scanner sc = new Scanner(System.in);int choice = sc.nextInt();switch (choice) {case 1: addstudent(students);break;case 2: delstudent(students);break;case 3: changestudent(students);break;case 4: squrestudent(students);break;case 5: {System.out.println("已退出学生管理系统");break loop;} default:System.out.println("没有这个选项,请重新输入:");}}}else{k++;System.out.println("验证码输入错误你还有"+(3-k)+"次机会");}}} else {j++;System.out.println("密码错误你还有"+(3-j)+"次机会");}}}}if (!yongHu){System.out.println("用户尚未注册或者你的密码多次错误或者验证码多次出错,请重试");}}private static String yanZengMa(){//验证码生成程序char []arr = new char[52];Random rand = new Random();for (int i = 0; i < 26; i++) {arr[i] = (char) (65+i);}for (int i = 0; i <26; i++) {arr[i+26] = (char) (97+i);}StringBuilder s = new StringBuilder();for (int i = 0; i < 4; i++) {char k = arr[rand.nextInt(52)];s.append(k);}//4个大小写1个数字int i = rand.nextInt(10);s.append(i);return s.toString();}private static void squrestudent(ArrayList<Student> students) {//查询遍历,(空学生表的判断)if (students.isEmpty()) {System.out.println("请添加后再查询");}else{System.out.println("id 姓名 年龄 住址");for (Student student : students) {System.out.println(student.getId() + " " + student.getName()+ " " + student.getAge() + " " + student.getAddress());}}}private static void changestudent(ArrayList<Student> students) {//更改学生表的成员,根据id信息更改if (students.isEmpty()) {System.out.println("请添加后再更改");}else{Scanner sc = new Scanner(System.in);System.out.println("输入id");String id = sc.next();if(!idtest(students, id)){System.out.println("id 匹配成功");System.out.println("输入姓名");String name = sc.next();System.out.println("输入年龄");int age = sc.nextInt();System.out.println("输入地址");String address = sc.next();for (Student student : students) {if (student.getId().equals(id)) {student.setId(id);student.setName(name);student.setAge(age);student.setAddress(address);System.out.println("更改成功");}}}elseSystem.out.println("无这个 id 信息,请重新选择你的需求");}}private static void delstudent(ArrayList<Student> students) {//删除学生表成员信息,要判断是否存在System.out.println("请输入id信息");Scanner sc = new Scanner(System.in);String id = sc.next();for (int i = 0; i < students.size(); i++) {if (students.get(i).getId().equals(id)) {students.remove(i);System.out.println("删除成功");break;}if ( i == students.size() - 1) {System.out.println("无这个学生信息,请添加之后再进行操作");}}}private static void addstudent(ArrayList<Student> students) {//添加学生表成员信息// System.out.println("分别输入 id 姓名 年龄 住址");
// Scanner sc = new Scanner(System.in);
// Student student = new Student(sc.next(),sc.next(),sc.nextInt(),sc.next());//添加方法一
// students.add(student);Student student = new Student();Scanner sc = new Scanner(System.in);//添加方法二System.out.println("输入id");while(true){String id = sc.next();if (idtest(students,id)) {student.setId(id);break;}elseSystem.out.println("id已经存在 请重新输入");}System.out.println("输入姓名");String name = sc.next();student.setName(name);System.out.println("输入年龄");int age = sc.nextInt();student.setAge(age);System.out.println("输入住址");String address = sc.next();student.setAddress(address);students.add(student);System.out.println("学生信息添加成功");}private static boolean idtest(ArrayList<Student> students,String id) { // 不存在返回truefor (Student student : students) {if (student.getId().equals(id)) {return false;}}return true;}}