Java中句子中最短和最长的单词

时间:2014-08-13 12:50:30

标签: java word

我是Java编程的新手!所以试图解决问题,找到句子中最短,最长的单词。我的程序如下所示。我希望你的人能告诉我关于我的计划的正确方向 -

public class StringProblems {
    void shortAndLongWord(String str) {
        String sw = "", lw = "";
        int s = str.length(), l = 0;
        String words[] = str.split(" ");

        for(String word:words) {
            if(word.length()<s)
                sw = word;
            else if(word.length()>l)
                lw = word;
        }

        System.out.println("LONGEST WORD : "+lw);
        System.out.println("SHORTEST WORD : "+sw);
    }

    public static void main(String[] args) {
        Scanner scr = new Scanner(System.in);

        StringProblems obj = new StringProblems();
        System.out.printf("Enter a line to get shortest and longest word:");
        String str = scr.nextLine();
        str += " ";
        obj.shortAndLongWord(str);
    }
}

此计划的输出是: *输入一行以获得最短和最快的词:这是句子

最长的词: 最简单的词:句子**

我不知道我的逻辑出错了!请帮帮我解决!

5 个答案:

答案 0 :(得分:3)

您必须不断更新当前最短且最长的单词的长度:

public class StringProblems {
    void shortAndLongWord(String str)
    {
        if (str == null)
            return;
        String sw="",lw="";
        int s=str.length(),l=0;
        String words[]=str.split(" ");
        for(String word:words)
        {
                if(word.length()<s)
                {
                        sw=word;
                        s = word.length();
                }
                if(word.length()>l)
                {
                        lw=word;
                        l = word.length();
                }
        }
        System.out.println("LONGEST WORD : "+lw);
        System.out.println("SHORTEST WORD : "+sw);
    }
    public static void main(String[] args) {
        Scanner scr=new Scanner(System.in);
        StringProblems obj=new StringProblems();
        System.out.printf("Enter a line to get shortest and longest word:");
        String str=scr.nextLine();
        str+=" ";
        obj.shortAndLongWord(str);
    }
}

答案 1 :(得分:2)

您需要不断更新l(最长字的长度)和s(最小字的长度)

if(word.length()<s)
{
        sw=word;
        s = word.length();
}
if(word.length()>l)
{
        lw=word;
        l = word.length();
}

此外,您还需要处理边界条件。 例如,如果字符串是单个单词会发生什么。当输入字符串为null等时会发生什么。

答案 2 :(得分:0)

在迭代期间,您没有更新ls的值。你应该尝试这样的事情:

if (word.length() < s) {
    sw = word;
    s = word.length();
} else if (word.length() > l) {
    lw = word;
    l = word.length();
}

答案 3 :(得分:0)

import java.util.Scanner;

public class ShortestAndLongestWordInALine {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter any sentence : ");
        String line = sc.nextLine();
        sc.close();
        int shortestWordLength = 0;
        int longestWordLength = 0;

        String shortestWord = "";
        String longestWord = "";

        int tempLength = 0;
        String[] eachWordArray = line.split(" ");
        boolean firstTime = false;
        for (String eachWord : eachWordArray) {
            tempLength = eachWord.length();
            if (firstTime == false) {
                firstTime = true;
                shortestWordLength = tempLength;
                shortestWord = eachWord;
                longestWordLength = tempLength;
                longestWord = eachWord;
            }

            if (tempLength > 0) {
                if (tempLength < shortestWordLength) {
                    shortestWordLength = tempLength;
                    shortestWord = eachWord;
                } else if (tempLength > longestWordLength) {
                    longestWordLength = tempLength;
                    longestWord = eachWord;
                }
            }
        }

        System.out.println("shortestWordLength = " + shortestWordLength + " shortestWord = " + shortestWord);
        System.out.println("longestWordLength = " + longestWordLength + " longestWord = " + longestWord);

    }

}

答案 4 :(得分:-1)

import java.util.*;

class lw

{

  public static void main()

{

 Scanner in=new Scanner(System.in);

      String z=" ",lw="";
        int q=0,o=0;
        System.out.println("Enter string");
        String str=in.nextLine()+" ";
        int l=str.length();
        for(int i=1;i<l;i++)
        {
            char ch=str.charAt(i);
            if(ch!=' ')
                z=z+ch; 
            else
            {
                q=z.length();                
                if(q>o)
                lw=z;
                o=q;
                z="";
            }
        }
        System.out.println("lw= "+lw);
        System.out.println("length="+q);
    }
}