将用户录入数据,包括:编号,密码,姓名,性别,电话号码进行有效性验证,无效则提示,有效且编号不重复的情况下添加到教师集合中Java代码实现
时间: 2024-03-05 18:51:23 浏览: 27
可以使用如下的Java代码实现:
```java
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Teacher {
private int id;
private String password;
private String name;
private String gender;
private String phone;
public Teacher(int id, String password, String name, String gender, String phone) {
this.id = id;
this.password = password;
this.name = name;
this.gender = gender;
this.phone = phone;
}
public int getId() {
return id;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public String getPhone() {
return phone;
}
public static void main(String[] args) {
Set<Integer> idSet = new HashSet<>();
Set<String> phoneSet = new HashSet<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入编号:");
int id = scanner.nextInt();
if (idSet.contains(id)) {
System.out.println("编号已存在,请重新输入!");
continue;
}
System.out.println("请输入密码:");
String password = scanner.next();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入电话号码:");
String phone = scanner.next();
if (phoneSet.contains(phone)) {
System.out.println("电话号码已存在,请重新输入!");
continue;
}
// 对数据进行有效性验证
// 编号必须大于0
if (id <= 0) {
System.out.println("编号无效,请重新输入!");
continue;
}
// 密码长度必须大于等于6
if (password.length() < 6) {
System.out.println("密码无效,请重新输入!");
continue;
}
// 姓名不能为空
if (name.isEmpty()) {
System.out.println("姓名无效,请重新输入!");
continue;
}
// 性别必须为男或女
if (!gender.equals("男") && !gender.equals("女")) {
System.out.println("性别无效,请重新输入!");
continue;
}
// 电话号码必须为11位数字
if (phone.length() != 11 || !phone.matches("\\d+")) {
System.out.println("电话号码无效,请重新输入!");
continue;
}
// 添加教师信息到集合中
Teacher teacher = new Teacher(id, password, name, gender, phone);
idSet.add(id);
phoneSet.add(phone);
System.out.println("教师信息添加成功!");
// 询问是否继续添加教师信息
System.out.println("是否继续添加教师信息?(Y/N)");
String choice = scanner.next();
if (choice.equalsIgnoreCase("N")) {
break;
}
}
}
}
```
上述代码中,我们使用了两个`Set`集合来分别存储已经存在的编号和电话号码,这样可以方便地进行编号和电话号码的重复检查。对于用户输入的数据,我们使用了一系列的逻辑运算符、正则表达式等方式进行有效性验证,确保输入的数据符合要求。如果输入的数据无效,程序会提示用户重新输入,直到输入的数据有效为止。如果输入的数据有效且编号不重复,程序会将教师信息添加到集合中,并提示用户添加成功。程序还支持多次添加教师信息,直到用户选择退出为止。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)