0

问题是 :

编写一个程序,读取用户 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 ");


}
}

我试着写我的代码,但他们总是兄弟,即使姓氏不一样!

4

5 回答 5

1

我想你想要

if ( lname1.equals(lname2))

代替

if ( name1.equals(name2))

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

于 2013-11-04T06:11:59.247 回答
1

尝试将您的问题分解为更小的问题,并为每个问题编写代码。例如,

  1. 你应该如何代表一个人的名字?每个人都有名字、中间名和姓氏吗?如果有人只输入 George Booth会发生什么?
  2. 你能写一个Name类吗?
  3. 什么是更好的变量名集?
  4. 你将如何测试你的代码?

另外,尽量不要在main. 它使事情难以测试。

于 2013-11-04T06:29:33.320 回答
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
于 2013-11-04T06:55:32.690 回答
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");
  }

}
于 2013-11-04T06:23:58.620 回答
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 
于 2013-11-04T06:30:49.447 回答