银行系统新增注册功能, 注册功 能要求用户名唯一, 不能与集合当中的任意一个账户的用户名相同,用户名具体如下: 1.用户名的长度需要是6 – 12位 2.用户名不能出现中文, 只能是数字或英文的组合, 可以是纯数字亦或者是纯字母的Java代码
时间: 2024-02-16 21:03:00 浏览: 15
好的,这里是实现要求的Java代码:
```java
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class BankSystem {
public static void main(String[] args) {
Set<String> usernames = new HashSet<String>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入用户名:");
String username = scanner.nextLine();
if (username.length() < 6 || username.length() > 12) {
System.out.println("用户名长度应为6-12位!");
continue;
}
if (!username.matches("^[a-zA-Z0-9]+$")) {
System.out.println("用户名只能包含数字或英文字母!");
continue;
}
if (usernames.contains(username)) {
System.out.println("该用户名已被注册,请重新输入!");
continue;
}
System.out.print("请输入密码:");
String password = scanner.nextLine();
System.out.println("注册成功!");
usernames.add(username);
System.out.print("是否继续注册?(Y/N)");
String choice = scanner.nextLine();
if (!"Y".equalsIgnoreCase(choice)) {
break;
}
}
scanner.close();
}
}
```
这个程序和之前的程序相比,去掉了密码格式的检查,并且在确认密码时也省略了对比的部分。因为这个问题只要求实现用户名的检查,而不需要检查密码。如果需要的话,可以在程序中加入密码的检查和确认密码的对比。