【问题标题】:I don't know how to define a variable for bubbleSort java我不知道如何为bubbleSort java定义一个变量
【发布时间】:2015-09-15 05:26:27
【问题描述】:

我正在制作一个计算我自己选择的数组的平均值(以双精度返回)、中位数(以双精度返回)、众数(以整数形式返回)和标准差的程序。我能找到的最有用的东西是数组是用户输入的代码。

我一直在使用这个和其他一些类似的 this guide 以及我的书和课堂笔记。有些东西我只是不断地修补它们,直到它们以某种方式起作用。

但就像我说的,在我的代码中,我只想将数组放入我自己,而不是从用户那里收集输入。我被困在中间。我已经全部输入了,但是编译器返回 1 个错误,上面写着:

发现 1 个错误: 文件:C:\Users\Cori\Desktop\Statistics.java [行:41] 错误:未定义统计类型的方法 bubbleSort(int[])

我完全按照链接所说的方式进行了bubbleSort,并且我一直在尝试各种疯狂的东西。我认为这可能与未定义的变量有关,但我真的不知道,因为这对我来说都很陌生。到目前为止,这是我的全部代码。我觉得如果我能弄清楚这一点,我的项目的其余部分将非常容易。

  public class Statistics {
    public static void main(String[] args) {
        int[] a = { 22, 44, 66, 55, 33 };

        double mean;
        double median;
        median = calcMed(a);
        mean = calcMean(a);
        System.out.println("Median:" + median);
        System.out.println("Mean:" + mean);
    }

    public static double calcMean(int[] a) {
        // int[]array = {22,44,66,55,33};
        int i;// =0;
        int sum = 0;
        double mean = 0;
        for (i = 0; i < a.length; i++) {
            System.out.println(a[i]);
            sum = sum + a[i];
        }
        {
            mean = ((double) sum / ((double) a.length));
            System.out.println();
        }
        {
            return mean;
        }
    }

    // Calulate median
    public static double calcMed(int[] a) {
        int i;
        int sum = 0;
        int[] sortedArr = bubbleSort(a);

        double median = 0;
        {
            int index = (sortedArr.length - 1) / 2;
            median = sortedArr[index];
        }
        for (int v : sortedArr) {
            System.out.println(v);
        }
        return median;
    }
}

请不要打扰我的格式(只是一些提示会很好)。我只需要知道如何修复bubbleSort,这样我就可以计算中位数。我也知道有些事情是不必要的,所以如果你也可以给我一些关于什么可以删除以及可能更容易的事情的指示。

我想通了。

【问题讨论】:

  • 你的bubbleSort()方法在哪里?
  • 你的代码中必须有一个bubblesort方法定义
  • 就像其他人所说的那样,您需要包含您的 bubbleSort 方法。您的错误消息指出名为 bubbleSort 的方法未定义,而不是变量。冒泡排序不是 Java 自动为您做的事情,您必须编写方法。请添加您的气泡排序代码,以便我们为您提供帮助。此外,如果您发现需要用户输入的示例,您仍然可以使用这些示例。只需删除用户输入行并将您创建的数组作为参数传递给方法。这有意义吗?
  • 我什至不知道那是什么。我需要一些重要的帮助。所以我做的 bubbleSort() 独立于中值方法?
  • 你需要在calcMed()方法中使用bubbleSort()方法。 sp calcMed() 依赖于 bubbleSort()

标签: java arrays mean bubble-sort median


【解决方案1】:

您缺少bubbleSort 方法(从相关链接复制):

/**
 * This program returns a sorted version of the input array.
 * 
 * @param arr
 * @return
 */
public static int[] bubbleSort(int[] arr)
{
    // We must sort the array.  We will use an algorithm called Bubble Sort.
    boolean performedSwap = true;
    int tempValue = 0;

    // If we performed a swap at some point in an iteration, this means that array
    // wasn't sorted and we need to perform another iteration
    while(performedSwap)
    {
        performedSwap = false;

        // Iterate through the array, swapping pairs that are out of order.
        // If we performed a swap, we set the "performedSwap" flag to true
        for (int i=0; i < arr.length; i++)
        {
            if (arr[i] > arr[i+1])
            {
                tempValue = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = tempValue;

                performedSwap = true;
            }
        }
    }

    return arr;
}

如果没有这种方法,您将无法对数组进行排序(有比冒泡排序更好的解决方案,但在这种情况下可以)。
错误:

发现 1 个错误:文件:C:\Users\Cori\Desktop\Statistics.java [行:41] 错误:该类型的方法 bubbleSort(int[]) 未定义 统计数据

告诉你缺少带有参数int[]的方法bubbleSort()

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2013-12-03
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多