Java 找到平均短的单词并全部打印出来

Java 找到平均短的单词并全部打印出来,java,arrays,split,Java,Arrays,Split,我需要写一个程序,找出给定字符串str中最短的单词 橄榄,鱼,追捕,警告,老,蟒蛇,爪哇,咖啡,猫,雷 伏特、橄榄、鱼、热追踪、警告、蟒蛇、爪哇、咖啡、零件 当我输入第一个迭代“olive、fish、pursuit、warning、old、python、java、coffee、cat、ray”时,它给出了[cat、old、ray] 但是当我输入第二次迭代“volt,olive,fish,hot pursuit,warning,python,java,coffee,part”时,它给了我“coff

我需要写一个程序,找出给定字符串str中最短的单词

橄榄,鱼,追捕,警告,老,蟒蛇,爪哇,咖啡,猫,雷

伏特、橄榄、鱼、热追踪、警告、蟒蛇、爪哇、咖啡、零件


当我输入第一个迭代“olive、fish、pursuit、warning、old、python、java、coffee、cat、ray”时,它给出了[cat、old、ray]

但是当我输入第二次迭代“volt,olive,fish,hot pursuit,warning,python,java,coffee,part”时,它给了我“coffee and python”,而需要输出[fish,java,part,volt]

你认为它与数组有关吗?排序

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] words = str.split(", ");
        Arrays.sort(words);
        String shortest = words[0];
        String count = "";

        for (int i = 0; i < words.length ; i++) {
            if(shortest.length() <= words[i].length() && words[i].length() == shortest.length()){
                shortest = words[i];
                count += shortest+", ";
            }
        }
        if(count.length() > 0){
            count = count.replaceAll(", $", "");
            System.out.print("["+count+"]");
        }
    }
}
import java.util.*;
公共班机{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
String str=scan.nextLine();
String[]words=str.split(“,”);
数组。排序(单词);
字符串最短=字[0];
字符串计数=”;
for(int i=0;i
您正在对单词进行排序,但我假设您希望按长度而不是字母顺序对它们进行排序,因此
单词[0]
确实是
最短的
。为此,您必须提供一个特定的
比较器

Arrays.sort(words, Comparator.comparing(String::length));
如果您想按字母顺序获得最短的单词,请先按长度排序,然后按自然(字母)顺序排序


现在剩下的代码可以工作了。但是,您的主循环有点多余。您不必检查长度是否为
要使用您的原始想法,您需要:

String shortest = words[0];
    String count = shortest;

    for (int i = 1; i < words.length ; i++) {
        if(shortest.length() == words[i].length()){//add word to shortest list
            shortest = words[i];
            count += ", " +shortest;
        }
        else if (words[i].length() < shortest.length())//found a shorter word
        {
            shortest = words[i];
            count = shortest;

        }
        //else word is longer, do nothing
    }
字符串最短=单词[0];
字符串计数=最短;
for(int i=1;i

如果你分别处理较短和相等的情况,跟踪和跟踪会更容易。而且,如果你已经看了第0个单词,你就不需要再看它了。

嗨,欢迎来到stackoveflow。你的问题是什么?嗨,我需要在给定输入的数组中找到所有最短的单词。谢谢!我可以吗你解释了什么在你当前的代码中不起作用?正如Nicolas所说,你具体面临的问题是什么?你当前的输出是什么,预期的输出是什么?当我输入第一次迭代时,它给出了“olive,fish,pursuit,warning,old,python,java,coffee,cat,ray”但当我输入第二次迭代时,即“volt,olive,fish,hot-pursuit,warning,python,java,coffee,part”,它给了我咖啡和python,而需要输出[fish,java,part,volt]非常感谢!!!!一切都解决了!最后一个问题tho;我怎样才能按字母顺序打印它们?@Mistersolmes使用
stream
,只需在两者之间添加
sorted()
。使用第一种方法,它有点复杂。伙计!你是最棒的!非常感谢!
for (int i = 0; i < words.length ; i++) {
    if (words[i].length() == shortest.length()){
        count += words[i] + ", ";
    }
}
for (int i = 0; i < words.length ; i++) {
    if (words[i].length() < shortest.length()){
        shortest = words[i];
        count = "";
    }
    if (words[i].length() == shortest.length()){
        count += words[i] + ", ";
    }
}
int len = Stream.of(words).mapToInt(String::length).min().getAsInt();
String count = Stream.of(words).filter(w -> w.length() == len)
        .sorted().collect(Collectors.joining(", "));
String shortest = words[0];
    String count = shortest;

    for (int i = 1; i < words.length ; i++) {
        if(shortest.length() == words[i].length()){//add word to shortest list
            shortest = words[i];
            count += ", " +shortest;
        }
        else if (words[i].length() < shortest.length())//found a shorter word
        {
            shortest = words[i];
            count = shortest;

        }
        //else word is longer, do nothing
    }