Java判断账号 邮箱 输入的是否正确的简单案例
发布日期:2021-06-29 15:01:51 浏览次数:3 分类:技术文章

本文共 1524 字,大约阅读时间需要 5 分钟。

是字母数组或是下换线

判断符合这种情况的然后取反,不满足这种情况的都是错误的

判断账号

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

转载地址:https://code100.blog.csdn.net/article/details/106576960 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Struts2的Action的配置和访问(大全)
下一篇:C语言算法,图解+详解 统计输入字符串当中要查找字符串的数量,substr所指的子符串在str所指的字符串中出现的次数。

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月27日 10时38分12秒