package Excercise;
public class ZhuCe {
public static void main(String[] args) {
String name="jack";
String pwd="121212";
String email="jack@123.com";
try {
userRegister(name,pwd,email);
System.out.println("恭喜你注册成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// tal 输入用户名、密码、邮箱,如果信息录入正确,则提示注册成功,否则生成异常对象要求:
// (1)用户名长度为2或3或4
// (2)密码的长度为6,要求全是数字'123456'
// (3)邮箱中包含@和.并且@在.的前面isDigital
//思路分析
//1.首先是编写一个方法,userRegister(String name,String pwd,String email){}
//2.针对输入的内容进行校核,如果发现有问题,就抛出异常,给出提示
//3.单独写一个方法,用来判断慢慢是否全部是数字字符,Boolean
//3.
//3.
public static void userRegister(String name, String pwd, String email) {
//第1关
int userLength = name.length();
if (!(userLength >= 2 && userLength <= 4)) {
throw new RuntimeException("用户名长度为2或3或4");
}
//第2关
if (!(pwd.length() == 6 && isDigital(pwd))) {
throw new RuntimeException("密码的长度为6,要求全是数字'123456'");
}
//第3关
int i=email.indexOf('@');
int j=email.indexOf('.');
if (!(i>0&&j>i)){
throw new RuntimeException("邮箱中包含@和.并且@在.的前面isDigital");
}
}
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;
}
}






网友评论