day4_work1_mailMethod

作业1

编写一个方法,实现从控制台接收一个字符串,并且验证该字符串是否符合邮箱格式,返回true或false。

代码

import java.util.Scanner;
public class JudgeMail {
    public static String mailAddress = null;
    public static void judge(String mailAddress){
        if(mailAddress.matches("[\\w]+@[\\w]+.[\\w]+")){
            System.out.println("输入邮箱格式正确");
        }
        else
            System.out.println("输入邮箱格式不正确");
    }//judge方法,判断邮箱合法性
    public static void main(String[] args){
        System.out.println("请输入邮箱");
        Scanner input = new Scanner(System.in);
        mailAddress = input.next();
        judge(mailAddress);
        input.close();
    }
}

运行结果


)

posted @ 2021-01-12 17:28  zzz2077  阅读(63)  评论(0编辑  收藏  举报