2

我正在尝试编写一个名为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();
    }

}

我的输出是:

输入姓名#1:roy
输入姓名#2:DANE
输入姓名 #3:Erik
输入名称 #4:sTeFaNiE
输入姓名 #5:LaurA
斯蒂芬妮的名字最长
 (有一条领带!)

它只会打印每次调用都有一条领带。我不知道为什么。第二,

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

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

4

3 回答 3

2

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

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

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

longest = longest.toLowerCase( );

现在longest全部小写。

Character.toUpperCase (longest.charAt( 0 ) )

这需要第一个字符并将其大写。

longest.substring(1);

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

于 2013-04-23T22:34:05.737 回答
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!)");
    }
}
于 2020-09-11T05:41:42.917 回答
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();
        }
    }
}
于 2019-09-08T10:36:19.867 回答