寻找最长的名字

时间:2013-04-23 22:32:03

标签: java

我正在尝试编写一个名为longestName的静态方法,该方法读取用户键入的名称并打印最长的名称(包含最多字符的名称)。

最长的名称应打印,其首字母大写,所有后续字母均为小写,无论用户在输入名称时使用的大写字母。如果两个或多个名称之间存在最长的平局,请使用最早键入的绑定名称。同时打印一条消息,说明存在平局,如下面的右侧日志中所示。 如果一些较短的名字与长度相关,例如DANE和Erik;但除非领带位于最长名称之间,否则不要打印消息。

public static void longestName(Scanner console, int n) {

    String name = "";
    String longest= "";
    boolean areTies = false;
    for(int i=1; i<=n; i++) {
        System.out.print("Enter name #" + i + ":");
        name = console.next();
        if(name.length( ) > longest.length( )) {

            longest = name;
            areTies = false;
        }
        if(name.length( ) == longest.length( ) ) {
            areTies = true;

        }
    }
    // now change name to all lower case, then change the first letter
    longest = longest.toLowerCase( );
    longest = Character.toUpperCase (longest.charAt( 0 ) ) + longest.substring(1);

    System.out.println(longest + "'s name is longest");
    if(areTies==true) {
        System.out.println(" (There was a tie! ) " );
    }else{
        System.out.println();
    }

}

我的输出是:

Enter name #1:roy
Enter name #2:DANE
Enter name #3:Erik
Enter name #4:sTeFaNiE
Enter name #5:LaurA
Stefanie's name is longest
 (There was a tie! ) 

它只会打印出每个调用都有一个平局。我不知道为什么。 其次,

longest = longest.toLowerCase( );
longest = Character.toUpperCase (longest.charAt( 0 ) ) + longest.substring(1);

我的朋友教我使用它来检索这个词,但我还是不明白。还有其他方法吗?这对我来说非常复杂。

3 个答案:

答案 0 :(得分:2)

您的逻辑存在问题。当您找到新的最长名称(第一个if语句)时,您将longest设置为name。然后第二个if执行。因为此时longest引用与name相同的对象,当然它们的长度是相等的。为避免这种情况,只需插入else

else if(name.length( ) == longest.length( ) ) {

让我们分解它是如何更改为第一个字符大写,其余为小写。

longest = longest.toLowerCase( );

现在longest都是小写的。

Character.toUpperCase (longest.charAt( 0 ) )

这需要第一个字符和大写字母。

longest.substring(1);

这将从索引1开始的子字符串(第二个字符)到字符串的末尾,该字符串与大写字符连接。

答案 1 :(得分:0)

import java.util.*;

public class Longest {

    public static void main(String[] args)
    {   Scanner input = new Scanner(System.in);  
        System.out.print("Enter the number of names you wanna enter? ");
        int n = input.nextInt();  // takes number of names 
        longestName(input,n);   // function call
    }
    public static void longestName(Scanner input, int n)
    {
        String name = "";  // First assign name to the empty string
        String longest = "";  // First assign longest to the empty string
        boolean areTies = false;  // Assume there are no ties at first place
        for(int i = 1; i <= n; i++)  // run a loop n times
        {
            System.out.print("Enter name #"+i+":");
            name = input.next();  // takes ith name
            if(name.length() > longest.length())  /*if length of the entered new string is greater than current longest*/
            {
                longest = name;
                areTies = false;  // no tie
            }
            else if(name.length() == longest.length())  /*if both strings are of same length*/
                areTies = true;  // so boolean value of areTies is true
        }
        // now change the name to the lower case and then change only first letter to the upper case
        longest = longest.toLowerCase(); // changes entire string to lowercase
        longest = Character.toUpperCase(longest.charAt(0)) + longest.substring(1);  /* first char to upper and rest remains as it is and concatenate char and remaining string from index 1 to rest*/

        System.out.println(longest + "'s name is longest");
        if(areTies==true) {
            System.out.println(" (There was a tie! ) " );
        }else{
            System.out.println();
        }
    }
}

答案 2 :(得分:0)

public static void longestName(Scanner console, int names) {
    String longest = "";
    boolean tie = false;
    for ( int i = 1; i <= names; i++){
        System.out.print("name #" + i + "? ");
        String name = console.nextLine();
        if (name.length() == longest.length()){
            tie = true;
        }
        if (name.length() > longest.length()){
            longest = name;
            tie = false;
        }
    }
    longest = longest.toLowerCase();
    System.out.print(longest.substring(0,1).toUpperCase());
    System.out.println(longest.substring(1) + "'s name is longest");

    if (tie){
       System.out.println("(There was a tie!)");
    }
}