2 回答
TA贡献1877条经验 获得超1个赞
我建议使用while循环。当for您知道需要重复操作多少次时,循环是最好的,但while循环适用于重复未知次数。它可以运行 0 到多次。
至于您的while循环条件,您只想检查电子邮件地址是否无效。因此,请检查!emailAddress.contains("@")它是否不包含@,!emailAddress.contains(".")如果不包含.。
您可以使用||来查看任一条件是否为真。
// Prompt the user
System.out.println("Please enter an email address");
String emailAddress = // read in their email adress
while(!emailAddress.contains("@") || !emailAddress.contains(".")) {
System.out.println("You have entered an invalid email address, please enter another.");
emailAddress = // read in their email address again
}
// Email address now contains "@" and "."
TA贡献1735条经验 获得超5个赞
像这样的东西应该工作:
// While email does not contain the two characters..
// Keep asking the user to enter the correct email.
while(!emailAddress.contains("@") || !emailAddress.contains(".")){
System.out.print("\nYou have entered an invalid email address.");
// Add line of code here to ask for email address.
// Update emailAddress of course
}
//prompt user for major & classification code
System.out.print("\nPleast enter two characters (Character #1: Major Code and Character #2: Classification
// ...
添加回答
举报