验证电子邮件地址后如何做 while 循环?

How to make do while loop after validating email address?

// My Scanner
Scanner input = new Scanner(System.in);   
//using Do While Loop
do {
    //Asking user to enter email
    System.out.println("enter your email:");
    //Read and safe input in to Var userEmail
    String userEmail = input.next();
    //Check for contains '@' and '.com' simbols
    Pattern pattern = Pattern.compile("\S+?@\S+?\.com");
    //And it checking in users entered email
    Matcher matcher = pattern.matcher(userEmail);
    //if userEmail contain '@'and '.com' print next line
    if (matcher.matches()) {
        System.out.println("Matches"); // Prints this for this email    
    }
    //if user put email with out '@'and'.com' print next line
    else {
        System.out.println("your email should 
        looks like this sample bob.Dillon@gmail.com");
    }
// And here I have a problem don't know what to type in
// so that it starts looping until user input will be 100% correct.
} while(!matcher.matches());

有人可以帮忙while(here);让它循环吗?

您想查看用户是否在这些字段中输入了任何内容。所以,像这样检查:

if (INPUTVALUE.length > 0) { //THEY ENTERED SOMETHING
    // do something
}

然后,将其放入您的 while 语句中。像这样:

// My Scanner
 Scanner input = new Scanner(System.in);   
    //using Do While Loop
    do{
        //Asking user to enter email
        System.out.println("enter your email:");
        //Read and safe input in to Var userEmail
        String userEmail = input.next();
        //Check for contains '@' and '.com' simbols
        Pattern pattern = Pattern.compile("\S+?@\S+?\.com");
        //And it checking in users entered email
        Matcher matcher = pattern.matcher(userEmail);
        //if userEmail contain '@'and '.com' print next line
        if (matcher.matches()) {
            System.out.println("Matches"); // Prints this for this email    
        }
        //if user put email with out '@'and'.com' print next line
        else{
            System.out.println("your email should 
            looks like this sample bob.Dillon@gmail.com");
        }
    //And here I have a problem don't know what to type in so that it starts looping until user input will be 100% correct 
    }while(INPUTVALUE.length > 0);

您需要:

}while(INPUTVALUE.length > 0);

打破循环:

只需删除用户在 do 结束时输入的所有值。这样,INPUTVALUE.length < 0。这将打破循环!祝你好运!