java - 验证电子邮件地址后如何进行 while 循环?

标签 java do-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(!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。这将打破循环!祝你好运!

关于java - 验证电子邮件地址后如何进行 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589876/

相关文章:

java - ObjectInputStream 中的 StreamCorruptedException

java - 导出/导入加密代码 Java

c++ - 将 int 与 string 和 getline 一起使用没有错误

c++ - 我需要线程函数在不停止实际程序的情况下每 2 秒打印一次随机数

c - 再玩一次函数c

java - 在 java 中填充数字直到字符串长度为 8 个字符?

java - 线程 "main"java.lang.NoSuchFieldError : filesystem 中的异常

c - 如何修复此 do-while 循环,以便控制台程序在自动关闭之前提示用户?

java - 我可以在运行时更改 Android 应用程序样式吗?

java - Action 后循环中断