#第三道
package 周末作业;
import java.util.Scanner;
public class 三 {
// 2、解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,
// 如果邮箱地址不合法则显示不合法的原因 [选做题]
// 2.1 提示:邮箱地址不合法的因素:
// 2.1.1邮箱地址中不包含@或.
// 2.1.2邮箱地址中含有多了@或.
// 2.1.3邮箱地址中.出现在@的前面
// 2.1.4用户名里有其他字符
// 2.2实现步骤:
// 2.2.1创建一个类,类名:mailtest
public static void main(String[] args) {
System.out.println("请输入一个邮箱地址……");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
if(TestMail(str) == true){
}else{
System.out.println("不合法");
}
}
public static boolean TestMail(String str){
boolean b = true;
if(str.indexOf('@') == -1 || str.indexOf('.') == -1){
System.out.println("邮箱地址中不包含@或.");
return false;
}
if(str.indexOf('@') != str.lastIndexOf('@') ||str.indexOf('.') != str.lastIndexOf('.')){
System.out.println("邮箱地址中含有多了@或.");
return false;
}
if(str.lastIndexOf('@') > str.indexOf('.')){
System.out.println("邮箱地址中.出现在@的前面");
return false;
}
for(int i = 0;i<str.indexOf('@');i++){
char c = str.charAt(i);
if( (c>=4&&c<=57) || (c>=65&&c<=90) || (c>=97&&c<=122) ){
}else{
System.out.println("邮箱用户名包含非法字符");
return false;
}
}
String[] array = str.split("@");
System.out.println("用户名为:"+array[0]);
System.out.println("该邮箱所属的网站域名为"+array[1]);
return true;
}
}