Java输入

时间:2018-05-08 01:08:25

标签: java string

我是初学者 - 新手,我想弄清楚为什么找到最大单词的逻辑不起作用。 有时输出将产生正确的最长单词,第一个单词或多个单词。

谢谢!

PS 如果有两个相同长度的单词,我真的不在乎这些情况,一旦我弄清楚为什么这不起作用,我将在以后工作。请再次注意我是初学者/新手。谢谢



import java.util.Scanner;
import java.util.ArrayList;
public class Word
{
    public static String word(String str)
    {
        int longestCount=0;
        int count=0;
        int newWord=0;
        String theWord="";
        ArrayList <String> longestWord= new ArrayList <String>();
        for (int i=0; i <str.length(); i++)
        {
            if (str.substring(i,i+1).equals(" "))
            {
                if (longestCount<count)
                {
                    longestCount=count;
                    theWord="";
                    theWord=""+str.substring(newWord,i);
                    newWord=i+1;
                    count=0;
                }
            }
            else
            {
                count++;
            }
        }
        longestWord.add(theWord);
        String output="";
        for (int i=0; i<longestWord.size();i++)
            output+=longestWord.get(i);
        return output;
    }

    public static void main ()
    {
        Scanner scan= new Scanner(System.in);
        String words= scan.nextLine();
        System.out.println(word(words));
    }
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

你在想它。只需循环遍历数组列表一次,只要看到更长的单词,就存储单词/或其索引:

ArrayList <String> words= new ArrayList <String>();
String currLongest = words.get(0);
for (String s : words)
    if(s.length() > currLongest.length())
        currLongest = s;

如果您的单词作为由空格分隔的单个String传递,则过程是相同的。只需在循环之前拆分它们:

String[] words = str.split(" ");
String currLongest = words.[0];
for (String s : words)
    if(s.length() > currLongest.length())
        currLongest = s;   

请注意,无需将最长的单词存储到列表中,因为在任何时候,应该只有一个最长的单词。

答案 1 :(得分:1)

首先使用拆分更容易切断字符串。然后,您可以将代码简化为以下内容。

我在下面的代码中尽可能多地评论

public static List<String> word(String str)
{
    String[] choppedUpWord = str.split(" ");
    int longestWordLength = 0; //we reset the longestWord if this is updated.
    ArrayList <String> longestWord= new ArrayList <String>(); //the results

    for(int i=0; i < choppedUpWord.length; i ++){
       String theWord = choppedUpWord[i];
       if(longestWordLength < theWord.length()){
           //new longest word found !
           longestWord.clear(); //remove the old entries
           longestWord.add(theWord); // add the new word in
           longestWordLength = theWord.length(); update with new length
       }else if(longestWordLength == theWord.length()){
           //same length as the longest word, do an appending.
           longestWord.add(theWord); // add the new word in 
       }
    } 

    return longestWord;
}

当多个单词长度相同时,它返回一个列表而不是该事件的String。

编辑或者您也可以使用StringBuilder。

public static String word(String str)
{
    String[] choppedUpWord = str.split(" ");
    int longestWordLength = 0; //we reset the longestWord if this is updated.
    StringBuilder longestWord= new StringBuilder(); //the results

    for(int i=0; i < choppedUpWord.length; i ++){
       String theWord = choppedUpWord[i];
       if(longestWordLength < theWord.length()){
           //new longest word found !
           longestWord.setLength(0); //remove the old entries
           longestWord.append(theWord); // add the new word in
           longestWordLength = theWord.length(); //update with new length
       }else if(longestWordLength == theWord.length()){
           //same length as the longest word, do an appending.
           longestWord.append(" "); //add a spacing between each word (or any delimiter that you like)
           longestWord.add(theWord); // add the new word in 
       }
    } 

    return longestWord.toString();
}