似乎无法让它工作不能像我想的那样把中间带取出来

jw5wzhpr  于 2021-07-08  发布在  Java
关注(0)|答案(4)|浏览(328)

我拿不出中间带。我要的是单词的中间值。很难从中获得价值 for 循环。

public class MedianWord {
    static double medianWordLength(String words) {
        String[] parts = words.split(" ");
        int[] a;
        double median = 0;

        for (int i = 0; i < parts.length; i++) {
            a = new int[parts[i].length()];
            Arrays.sort(a);
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            }
            else
                median = (double) a[a.length / 2];
        }
        return median;
    }
}
0qx6xfy6

0qx6xfy61#

public static double medianWordLength(String str) {
    int[] arr = Arrays.stream(str.split("\\s+"))
                      .mapToInt(String::length)
                      .sorted()
                      .toArray();

    int mid = arr.length / 2;
    double median = (double)arr[mid];

    return arr.length % 2 == 0 ? (median + arr[mid - 1]) / 2 : median;
}
4dbbbstv

4dbbbstv2#

这应该很管用:

static Integer getMedian(String sentence) {
        String[] str = sentence.split(" ");
        Integer[] strLen = new Integer[str.length];

        for (int i = 0; i < strLen.length; i++) {
            strLen[i] = str[i].length();
        }

        return strLen.length % 2 == 0?
                (strLen[strLen.length / 2] + strLen[strLen.length / 2 - 1]) / 2 : strLen[strLen.length / 2];
    }
mznpcxlj

mznpcxlj3#

循环的每次迭代都会覆盖 a 使用新数组的数组。您需要将问题分成两部分-首先,迭代 parts 数组并将其转换为数组 a 然后找到数组的中值:

static double medianWordLength(String words) {
    String[] parts = words.split(" ");
    int[] a = new int[parts.length];
    for (int i = 0; i < parts.length; i++) {
        a[i] = parts[i].length();
    }

    Arrays.sort(a);
    if (a.length % 2 == 0) {
        return ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
    }
    else {
        return a[a.length / 2];
    }
}

旁注:
将单词转换成一个排序的长度数组可以(可以说)用流更优雅地完成:

int[] a = Arrays.stream(words.split(" ")).mapToInt(String::length).sorted().toArray();
ct3nt3jp

ct3nt3jp4#

首先,不断地对数组进行排序,然后在循环中替换数组。这是你的主要问题。
但是为了简化这个过程,您只需要一个数组而不需要for循环。只需根据单词的长度对单词数组进行排序。我留了一些打印报表在里面,这样你就可以看到发生了什么。如果在一个或多个空格上拆分,则在输入字符串时无需如此小心。

String quote = "To be or not to be that is the question";
System.out.println(medianWordLength(quote));

static double medianWordLength(String words) {
    String[] parts = words.split("\\s+");
    double median = 0;

    // sort the array based on length of the words
    Arrays.sort(parts,
            (a, b) -> Integer.compare(a.length(), b.length()));

    for (String v : parts) {
        System.out.println(v);
    }
    System.out.println("------------------");

    // get the "middle" index.
    int idx = parts.length / 2;

    // adjust index based on array size
    if (parts.length % 2 == 0) {
        System.out.println(parts[idx-1] + " " + parts[idx]);
        median = (parts[idx-1].length() + parts[idx].length())
                / 2.;
    } else {
        System.out.println(parts[idx]);
        median = parts[idx + 1].length();
    }
    return median;
}

印刷品

To
be
or
to
be
is
not
the
that
question
----------
be is
2.0

相关问题