【问题标题】:Shortest and Longest word in a sentence in JavaJava句子中最短和最长的单词
【发布时间】:2014-08-13 12:50:29
【问题描述】:

我是 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);
    }
}

这个程序的输出是: *输入一行得到最短和logest word:This is Sentence

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

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

【问题讨论】:

  • 一旦你的当前实现技术发挥作用,尝试创建字符串数组来解决这个问题。比较速度上的差异。
  • 我也一定会尝试的..

标签: java word


【解决方案1】:

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

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);
    }
}

【讨论】:

  • nitpick,但它应该是 if 而不是 else if,因为最长和最短的单词可能相同。
  • 感谢@KevinDiTraglia 在更改 else if 后如果程序运行良好..! Ans 感谢您在代码 jh314 中的输入!
【解决方案2】:

你需要不断更新l(最长单词的长度)和s(最小单词的长度)

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

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

【讨论】:

  • 真正的问题,空字符串会返回word.Length() == 0吗?或者这会导致Null Reference Exception
  • 我做到了。问题现在解决了一半,因为运行后我发现 - 输入一行以获取最短和最长的单词:hkkh kk hm 最长单词:hm 最短单词:kk(抱歉随机检查!但最高单词应该是'hkkh'对吗?)
  • @McAdam331 NullPointerException
  • 有道理。我重读了您的评论并误解了。 string.Length() 是否可能等于 0?也许是String s = "";
  • 没错。对于空字符串,长度为 0。
【解决方案3】:
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】:

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

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

    【讨论】:

    • 在改变 else if 之后如果程序运行良好.. !感谢您的输入 !我完全错过了 word.length() 部分!
    【解决方案5】:
    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 2021-04-20
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 2017-03-30
      相关资源
      最近更新 更多