注册一个用户名 要求:长度不小于6 密码相同

package com.sxt;

import java.util.Scanner;

/**
* 注册的练习
*
* @author Administrator
*
*/
public class Register {
public boolean verify() {
System.out.println(“欢迎使用注册系统“);
Scanner sc = new Scanner(System.in);
System.out.println(“请输入用户名:”);
String username = sc.next();
System.out.println(“请输入密码:”);
String userpwd = sc.next();
System.out.println(“请再次输入密码:”);
String pwdAgain = sc.next();
boolean falg = true;
// 验证用户名
if (username.length() < 3) {
System.out.print(“用户名长度不能小于3 “);
falg = false;
}
// 验证密码
if (userpwd.length() < 6) {
System.out.print(“密码长度不能小于6 “);
falg = false;
}
// 验证两次密码
if (!userpwd.equals(pwdAgain)) {
System.out.print(“两次密码不同”);
falg = false;
}
return falg;
}

public static void main(String[] args) {
    Register r = new Register();
    boolean b  = false;
    do {
        b = r.verify();
    } while (!b);
    System.out.println("注册成功");

}

}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个涉及到数组、字符串和逻辑判断的问题。首先需要定义一个数组来存储已经注册用户名,然后在用户注册时,检查用户名是否已经存在于数组中,如果存在,就提示用户换一个用户名;如果不存在,就将用户名添加到数组中,并要求用户输入密码,检查密码长度和组成是否符合要求。以下是一个示例代码: ```vb Dim registeredUsers() As String '定义存储已注册用户名的数组 Sub Register() Dim userName As String Dim password As String Dim hasUpperCase As Boolean Dim hasLowerCase As Boolean Dim hasNumber As Boolean Dim hasSpecialChar As Boolean '获取用户输入的用户名密码 userName = InputBox("请输入用户名:") password = InputBox("请输入密码:") '检查用户名是否已经存在 For i = 0 To UBound(registeredUsers) If registeredUsers(i) = userName Then MsgBox "该用户名已经存在,请换一个用户名!" Exit Sub End If Next '检查密码长度和组成是否符合要求 If Len(password) < 8 Then MsgBox "密码长度不得小于8位!" Exit Sub End If For i = 1 To Len(password) If Asc(Mid(password, i, 1)) >= 65 And Asc(Mid(password, i, 1)) <= 90 Then '检查是否有大写字母 hasUpperCase = True End If If Asc(Mid(password, i, 1)) >= 97 And Asc(Mid(password, i, 1)) <= 122 Then '检查是否有小写字母 hasLowerCase = True End If If Asc(Mid(password, i, 1)) >= 48 And Asc(Mid(password, i, 1)) <= 57 Then '检查是否有数字 hasNumber = True End If If Not ((Asc(Mid(password, i, 1)) >= 65 And Asc(Mid(password, i, 1)) <= 90) _ Or (Asc(Mid(password, i, 1)) >= 97 And Asc(Mid(password, i, 1)) <= 122) _ Or (Asc(Mid(password, i, 1)) >= 48 And Asc(Mid(password, i, 1)) <= 57)) Then '检查是否有特殊字符 hasSpecialChar = True End If Next If Not (hasUpperCase And hasLowerCase And hasNumber And hasSpecialChar) Then MsgBox "密码必须包含大写字母、小写字母、数字和特殊字符!" Exit Sub End If '将用户名添加到数组中 ReDim Preserve registeredUsers(UBound(registeredUsers) + 1) registeredUsers(UBound(registeredUsers)) = userName MsgBox "注册成功!" End Sub Sub Login() Dim userName As String Dim password As String Dim isRegistered As Boolean '获取用户输入的用户名密码 userName = InputBox("请输入用户名:") password = InputBox("请输入密码:") '检查用户名是否已经存在 For i = 0 To UBound(registeredUsers) If registeredUsers(i) = userName Then isRegistered = True Exit For End If Next If Not isRegistered Then MsgBox "该用户名尚未注册!" Exit Sub End If '检查密码是否正确 '这里可以根据实际情况,将密码存储到数组或文件中,然后进行比较 If Len(password) < 8 Then MsgBox "密码错误!" Exit Sub End If Dim hasUpperCase As Boolean Dim hasLowerCase As Boolean Dim hasNumber As Boolean Dim hasSpecialChar As Boolean For i = 1 To Len(password) If Asc(Mid(password, i, 1)) >= 65 And Asc(Mid(password, i, 1)) <= 90 Then '检查是否有大写字母 hasUpperCase = True End If If Asc(Mid(password, i, 1)) >= 97 And Asc(Mid(password, i, 1)) <= 122 Then '检查是否有小写字母 hasLowerCase = True End If If Asc(Mid(password, i, 1)) >= 48 And Asc(Mid(password, i, 1)) <= 57 Then '检查是否有数字 hasNumber = True End If If Not ((Asc(Mid(password, i, 1)) >= 65 And Asc(Mid(password, i, 1)) <= 90) _ Or (Asc(Mid(password, i, 1)) >= 97 And Asc(Mid(password, i, 1)) <= 122) _ Or (Asc(Mid(password, i, 1)) >= 48 And Asc(Mid(password, i, 1)) <= 57)) Then '检查是否有特殊字符 hasSpecialChar = True End If Next If Not (hasUpperCase And hasLowerCase And hasNumber And hasSpecialChar) Then MsgBox "密码错误!" Exit Sub End If MsgBox "登录成功!" End Sub ``` 以上代码只是一个示例,实际应用中还需要考虑更多的情况,比如将用户名密码存储到文件中、加密密码等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值