用正则表达式
import java.util.Scanner;public class Text1 {//目标:校验用户输入的手机号码,邮箱号码,座机号码正确public static void main(String args[]) {checkPhone();checkEmail();checkTel();}public static void checkPhone() {Scanner sc = new Scanner(System.in);while(true) {System.out.println("请输入手机号码:");String phoneNumber = sc.nextLine();if(phoneNumber.matches("1[3-9]\\d{9}")) {System.out.println("注册成功!");break;}else {System.out.println("格式有误!");}}}public static void checkEmail() {Scanner sc = new Scanner(System.in);while(true) {System.out.println("请输入邮箱号码:");String phoneNumber = sc.nextLine();if(phoneNumber.matches("\\w{1,30}@\\w{2,20}(\\.[0-9A-Za-z]{2,20}){1,2}") ){System.out.println("注册成功!");break;}else {System.out.println("格式有误!");}}}public static void checkTel() {Scanner sc = new Scanner(System.in);while(true) {System.out.println("请输入座机号码:");String phoneNumber = sc.nextLine();if(phoneNumber.matches("0\\d{2,6}-?\\d{5,20}")){System.out.println("注册成功!");break;}else {System.out.println("格式有误!");}}}}
编写程序模拟用户输入手机号码 邮箱号码 座机号码 验证格式是否正确并给出提示 直到格式输入正确为止