jgrasp。需要帮助。无法弄清楚如何编码

时间:2014-10-14 23:33:24

标签: java jgrasp

电子邮件地址包含@字符。编写一个程序,从键盘上取一个单词,并输出是否是基于@字符存在的电子邮件地址。不要担心这个词还有什么。

3 个答案:

答案 0 :(得分:2)

import java.util.Scanner;
  public class EmailAddress {
    public static void main(String[] args) { 
      Scanner input = new Scanner(System.in);
      System.out.print(" Enter a word: " );
      String word = input.nextLine();
      int count = 0;
      for(int i = 0; i < word.length(); i++) {
        if(word.charAt(i) == '@')
          count++;
      }
      if(count > 0) 
        System.out.println("Its an E-mail ");
      else
        System.out.println("Its not an E-mail ");
   }
 }     

答案 1 :(得分:0)

 import  java.util.Scanner;                                //Imports the scanner function

public class TestingCode {                                 //standard java input
 public static void main(String args[]) {                  //standard java input
  Scanner scan = new Scanner (System.in) ;                 //asks the user to input the email they would like to find the domain for
    System.out.print ( "What is the email? : " );
    String email = scan.next();                            //scans for the next string
    int index = email.indexOf('@') ;                       //finds the index at which the @ is located at and stores it as index
    String domain = email.substring (index , email.length() ); //uses the substring function to cut out everything before the @ symbol and stores that snippet as variable domain .
    System.out.print("The email domain is " + domain );     //displays variable domain


  }
}

答案 2 :(得分:0)

我不知道为什么你们都使代码如此复杂?!

import java.util.Scanner;
import java.util.*; //I Always import this as a good habit
public class EmailAddress
{
    public static void main(String[] args)
    { 
        Scanner input = new Scanner(System.in);
        System.out.print(" Enter a word: " );
        String word = input.nextLine();
        if(word.contains('@'))
        {
            System.out.println("It's an Email");
        }
        else
        {
            System.out.println("It's not an Email");
        }
    }
}