java(一個簡單郵箱的判斷)使用者從命令列輸入一個字串,要求判斷是否為合法的email地址,如果合法,請判斷是否為sina的郵箱地址
阿新 • • 發佈:2019-02-01
package com.tjetc.daomain;
import java.util.Scanner;
public class Work05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("請輸入合法的新浪郵箱:");
String str = input.nextLine();
int count=0;//定義一個計數器用來記錄@的個數
int count2=0;//定義一個計數器用來記錄.的個數
int x=0;//用來記錄出現第一個@對應的索引
int y=0;//用來記錄出現第一個.對應的索引
for(int j = 0;j<str.length()-1;j++){
String str1 =str.substring(j,j+1);
if(str1.equals("@")){
count++;
x=j;
}
if (str1.equals(".")){
count2++;
y=j;
}
//continue;
}
if(count==1 && count2==1 && x<(y-1) && x!=0 && y!=str.length()-1 ){
str.endsWith("@sina.com");
System.out.println("合法");
}else {
System.out.println("不合法");
}
}
}