判断两个名字是不是兄弟

时间:2013-11-04 06:09:33

标签: java string loops coding-style

问题是:

  

编写一个程序,读取用户2的全名,然后判断2是否为兄弟。

     

示例运行:

     

输入2个名字:

- First name:  Ali Ibrahim Mohammed
- Second name: Ahmad Ibrahim Mohammed
     

Ali and Ahmad are brothers

import java.util.*;

public class Test
{
static Scanner scan = new Scanner (System.in);

   public static void main(String args[])
    {
String name1 = ""; //the name1 without first name
String name2 = ""; //the name2 without first name

String firstname1="";//only the first name of name1
String firstname2="" ;//only the fist name of name2

String fname ="";
String lname ="";

String string, string2;

int space ;
int i =0;
 int j=0;

System.out.println("Enter 2 names :");
while ( i<2 )
{
if (i==0)
    System.out.println("- First name :"); 
    else
        System.out.println("- Second name :"); 

  while (j==i)
   {
    string = scan.nextLine();

    space= string.indexOf(" ");
    fname = string.substring(0,space);

    string2 = string.toLowerCase();  
    lname = string.substring(space);

    string2 = lname.toLowerCase(); 
    j++;

  }
     if(i==0){
     firstname1=fname;
     lname=name1;
     }

     else if(i==1){
     firstname2=fname;
     lname=name2;
     }

    i++;
}

if ( name1.equals(name2))
System.out.println(firstname1 + " and " + firstname2 + " are brothers ");
else 
System.out.println(firstname1 + " and " + firstname2 + " are NOT brothers ");


}
}

我试着编写我的代码,但即使名字不一样,他们也总是兄弟!

5 个答案:

答案 0 :(得分:1)

我想你想要

if ( lname1.equals(lname2))

而不是

if ( name1.equals(name2))

可能还有其他问题,但这至少是其中之一。

答案 1 :(得分:1)

尝试将问题分解为较小的问题,并对每个问题进行编码。例如,

  1. 你应该如何代表一个人的名字?每个人都有名字,中间名和姓吗?如果有人只是键入 George Booth
  2. 会发生什么
  3. 你能写一个Name班吗?
  4. 什么是更好的变量名称?
  5. 您如何测试代码?
  6. 另外,尽量不要在main中做所有事情。这使得测试变得困难。

答案 2 :(得分:1)

我使用一个标记器实现了这个,使用这种方法,程序能够找到多个人是兄弟。这是如何工作的,我们将人名全名存储在ArrayList中。然后,我们使用标记化器分解这些名称,并将最后一个名称存储到新数组中。然后,我们检查每个具有相同姓氏的人,并显示他们是兄弟。

   List<String> names = new ArrayList <String>();

    Scanner scanner = new Scanner(System.in);

    //gets user name
    for (int x = 0; x < 4; x++)
    {
        System.out.printf("Enter Full Name #%d: ", (x + 1));
        names.add(scanner.nextLine());
    }
    String [] lastName = new String [names.size()];

    //gets lastName
    for (int x = 0; x < names.size(); x++)
    {
        StringTokenizer token = new StringTokenizer(names.get(x));
        while (token.hasMoreTokens())
                lastName[x] = token.nextToken();
    }

    //check for brothers
    for (int x = 0; x < names.size(); x++)
        for (int i = x + 1; i < names.size();i++)
        {
            if (lastName[x].equalsIgnoreCase(lastName[i]))
                System.out.printf("%s and %s are brothers",names.get(x),names.get(i));
        }

输出:

 Enter Full Name #1: Andree Freemantle
 Enter Full Name #2: Mario Dennis
 Enter Full Name #3: Kyle Freemantle
 Enter Full Name #4: Steve dennis
 Andree Freemantle and Kyle Freemantle are brothers 
 Mario Dennis and Steve dennis are brother

答案 3 :(得分:0)

请参考以下修改版本的代码,希望这能解决您的问题。

public static void main(String[] args) {
    String n1="Ali Ibrahim Mohammed";
  String n2="Ahmad Ibrahim Mohammed";

  String name1FirstName=n1.split(" ")[0]; 
  String name2FirstName=n2.split(" ")[0];

  String name1LastName=n1.split(" ")[2]; 
  String name2LastName=n2.split(" ")[2];

  String name1MiddleName=n1.split(" ")[1]; 
  String name2MiddleName=n2.split(" ")[1];

  if(name1LastName.equals(name2LastName)){ // Check if Last name is same or not

      if(name1MiddleName.equals(name2MiddleName)){ // Check if middle name is same or not
          System.out.println(name1FirstName+"  and "+name2FirstName+" are brothers");
      }else{
          System.out.println(name1FirstName+"  and "+name2FirstName+" are not brothers");
      }



  }else{
      System.out.println(name1FirstName+"  and "+name2FirstName+" are not brothers");
  }

}

答案 4 :(得分:0)

    import java.util.*;

     class Test
    {
    static Scanner scan = new Scanner (System.in);

       public static void main(String args[])
        {
    //String name1 = ""; //the name1 without first name
    //String name2 = ""; //the name2 without first name

    String firstname1="";//only the first name of name1
    String firstname2="" ;//only the fist name of name2

    String fname ="";
    String lname ="";
    String lname1 ="";
    String lname2 ="";

    String string, string2;

    int space ;
    int i =0;
     int j=0;

    System.out.println("Enter 2 names :");
    while ( i<2 )
    {
    if (i==0)
        System.out.println("- First name :"); 
        else
            System.out.println("- Second name :"); 

      while (j==i)
       {
        string = scan.nextLine();

        space= string.indexOf(" ");
        fname = string.substring(0,space);

        string2 = string.toLowerCase();  
        lname = string.substring(space);

        string2 = lname.toLowerCase(); 
        j++;

      }
         if(i==0){
         firstname1=fname;
         lname1=lname;
         }

         else if(i==1){
         firstname2=fname;
         lname2=lname;
         }

        i++;
    }

    if ( lname1.equals(lname2))
    System.out.println(firstname1 + " and " + firstname2 + " are brothers ");
    else 
    System.out.println(firstname1 + " and " + firstname2 + " are NOT brothers ");


    }
    }

输出: -

    Enter 2 names :
    - First name :
    Ali Ibrahim Mohammed
    - Second name :
    Ahmad Ibrahim Mohammed
    Ali and Ahmad are brothers 

retry:==

    Enter 2 names :
    - First name :
    Ahmad Ibrahim 
    - Second name :
    Ali Ibrahim Mohammed]
    Ahmad and Ali are NOT brothers