输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象,要求:
(1)用户名长度为2
或3
或4
(2)密码的长度为6
,要求全是数字、
(3)邮箱中包含@
和.
@
在.
之前
public class Register{
public static void main(String[] args) {
String name = "jack";
String pwd = "382785";
String email = "jack@163.com";
try {
register(name,pwd,email);
System.out.println("注册成功");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//注册方法
public static void register(String name,String pwd,String email){
if(!(name != null && pwd != null && email != null)){
throw new RuntimeException("参数不能为空!");
}
int n = name.length();
if(!(n >= 2 && n <= 4)){
throw new RuntimeException("用户名长度为2或3或4");
}
if(!(pwd.length() == 6 && isDigital(pwd))){
throw new RuntimeException("密码的长度为6并且全是数字");
}
int i1 = email.indexOf("@");
int i2 = email.indexOf(".");
if(!(i1 > 0 && i1 < i2)){
throw new RuntimeException("邮箱格式不正确!");
}
}
//判断密码是数字方法
public static boolean isDigital(String str){
char[] chars = str.toCharArray();
for(int i = 0;i < chars.length; i++){
if(chars[i] < 0 && chars[i] > 9){
return false;
}
}
return true;
}
}