该程序检查输入的电子邮件地址是否包含@和.edu。
如果不是这样,则需要返回到步骤,我想我可以使用do-while,但是我还没有一个可以工作的,如何将if-else语句嵌套在do-while中?谢谢!

if (UserEmail.contains("@")) {
    if (UserEmail.contains(".edu")){
        System.out.print("Please create a password: ");
        PassWord = kb.nextLine();
        System.out.println(UserEmail.replaceAll("@\\S+?\\.edu\\b", ""));
        System.out.print("Your password is " + PassWord);
    } else {
        System.out.print("email is not valid Please, try again.")
    } else {
        System.out.print("email is not valid Please, try again.");
        // at this point it should repeat and ask for the email again
    }
}

最佳答案

为了简化您的if-else代码,请考虑使用

if (UserEmail.contains("@") && UserEmail.contains(".edu")) {
 .
 .
}

可以包装在do - while
do {
  if (UserEmail.contains("@") && UserEmail.contains(".edu")) {
  .
  .
  break;
  }
  System.out.print("email is not valid Please, try again.")
}
while (true);

10-06 01:48