是字母数组或是下换线
判断符合这种情况的然后取反,不满足这种情况的都是错误的
判断账号
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("输入账号:");
String username = sc.next();
boolean f = false;
for (int i = 0; i < username.length(); i++) {
char a = username.charAt(i);
if (!(a >= 'A' && a <= 'z' || a >= '0' && a <= '9' || a == '_')) {//判断符合这种情况的然后取反,不满足这种情况的都是错误的
f = false;
} else {
f = true;
}
}
if (f) {
System.out.println("格式正确");
} else {
System.out.println("格式错误");
}
}
}
判断邮箱
public class Test04 {
public static void main(String[] args) {
System.out.println("请输入邮箱");
Scanner sc = new Scanner(System.in);
String email = sc.next();
if (email.indexOf("@") == -1 || email.indexOf(".") == -1) {
System.out.println("邮箱格式必须包含@和.");
} else if (!(email.indexOf("@") == email.lastIndexOf("@") || email.indexOf(".") == email.lastIndexOf("."))) {
System.out.println("邮箱格式必须只能包含一个@和.");
} else if (email.indexOf("@") == 0 || email.indexOf("@") == email.length() - 1 || email.indexOf("@") == 0
|| email.indexOf("@") == email.length() - 1) {
System.out.println("邮箱格式@和.不能在最后和最前面");
} else if (email.lastIndexOf("@", email.indexOf(".")) == email.indexOf(".") - 1) {
System.out.println("邮箱格式.前面不能有@");
} else if (!(email.indexOf("com", email.indexOf(".")) == email.indexOf(".") + 1)) {
System.out.println("邮箱格式.后面必须是com");
} else {
System.out.println("邮箱格式正确");
}
}
}
在实际应用当中将上访的if else if 换成&& 即可