输入用户名、密码、邮箱、如果信息录入正确,则提示注册成功,否则生成异常:
要求:
- public static void main(String[] args) {
- try {
- inputMessage("扎金花","124l75","dasda@.");
- inputMessage("小卤蛋","123456","@.sdasdadf");
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
-
- public static void inputMessage(String name, String pwd, String e_mail) {
- //继续优化,加入判断是否为null的异常捕获
- if(!(name!=null&&pwd!=null&&e_mail!=null)){
- throw new RuntimeException("输入信息不能为空!");
- }
- if (!(name.length() >= 2 && name.length() <= 3)) {
- throw new RuntimeException("输入用户名格式不正确!");
- }
- char[] bufChar = pwd.toCharArray();
- boolean isprime = true;//密码报错
- for (int i = 0; i < bufChar.length; i++) {
- if (bufChar[i] > '9' || bufChar[i] < '0') {//判断不是数字
- isprime = false;
- break;
- }
- }
- if (!(isprime==true&&pwd.length()==6)){
- throw new RuntimeException("输入的密码格式不正确!");
- }
- if(!(e_mail.indexOf('@')!=-1&&(e_mail.indexOf('@')
'.')))){ - throw new RuntimeException("输入的邮箱不正确!");
- }
- System.out.println("恭喜您!注册成功。");
- }
- }
其中,判断密码是否全为数字可以,可以这样:
- public static boolean isprime(String str){
- char[] bufChar = str.toCharArray();
- for (int i = 0; i < bufChar.length; i++) {
- if (bufChar[i] > '9' || bufChar[i] < '0') {//判断不是数字
- return false;
- }
- }
- return true;
- }
也可以这样:
- char[] bufChar = pwd.toCharArray();
- boolean isprime = true;//密码报错
- for (int i = 0; i < bufChar.length; i++) {
- if (bufChar[i] > '9' || bufChar[i] < '0') {//判断不是数字
- isprime = false;
- break;
- }
- }
打印结果:
