Java判断账号 邮箱 输入的是否正确的简单案例

是字母数组或是下换线
判断符合这种情况的然后取反,不满足这种情况的都是错误的

判断账号


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 换成&& 即可

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员猫爪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值