简体   繁体   English

我不知道如何为bubbleSort java定义变量

[英]I don't know how to define a variable for bubbleSort java

I am making a porgram that calculates the Mean(return as double), Median(return as double), Mode(return as int), and Standard deviation of an array that I chose myself. 我正在制作一个计算均值(返回为double),中位数(返回为double),Mode(返回为int)和我自己选择的数组的标准差的porgram。 The most helpful things I can find are codes where the array is an input by the user. 我能找到的最有用的东西是代码,其中数组是用户输入的。

I have been using this and some other ones similar as kind of this guide as well as my book and notes from class. 我一直在使用这个和其他一些类似于本指南以及我的书和课堂笔记。 Some things I just keep tinkering with them until they somehow work. 有些事我只是在修补它们,直到它们以某种方式工作。

But like I said, in my code I would just like to put the array in myself and not gather input from the user. 但就像我说的那样,在我的代码中,我只想将数组放入自己,而不是从用户那里收集输入。 I am stuck on the median. 我被困在中位数。 I have it all typed up but the compiler is throwing back 1 error that says: 我把它全部打了,但编译器抛出了1个错误,说:

1 error found: File: C:\\Users\\Cori\\Desktop\\Statistics.java [line: 41] Error: The method bubbleSort(int[]) is undefined for the type Statistics 找到1个错误:文件:C:\\ Users \\ Cori \\ Desktop \\ Statistics.java [line:41]错误:方法bubbleSort(int [])未定义类型统计信息

I did the bubbleSort exactly like the link says and I have been trying all kinds of crazy stuff. 我完成了链接所说的bubbleSort,我一直在尝试各种疯狂的东西。 I think maybe It has something to do with the variable not being defined, but I really don't know because this is all very foreign to me. 我想也许它与未定义的变量有关,但我真的不知道,因为这对我来说都是非常陌生的。 Here is my entire code so far. 到目前为止,这是我的整个代码。 I feel like if I can just figure this out the rest of my project will be very easy. 我觉得如果我能想到这一点,我的项目的其余部分将非常容易。

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

Please don't dog my formatting(just some tips would be nice). 请不要欺骗我的格式(只是一些提示会很好)。 I just need to know how to fix the bubbleSort so I can calculate the median. 我只需要知道如何修复bubbleSort所以我可以计算中位数。 Also I know some things are unnecessary, so if you could also give me some pointers on what is ok to delete and things that might be easier. 另外我知道有些事情是不必要的,所以如果你也可以给我一些关于什么可以删除以及可能更容易的事情的指示。

I figured it out. 我想到了。

You are missing the bubbleSort method (copied from the link in question): 您缺少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;
}

Without this method you can't sort the array (there are more better solutions then bubblesort, but for this case ok). 如果没有这种方法,你就无法对数组进行排序(还有更好的解决方案,然后是bubblesort,但对于这种情况确定)。
The error: 错误:

1 error found: File: C:\\Users\\Cori\\Desktop\\Statistics.java [line: 41] Error: The method bubbleSort(int[]) is undefined for the type Statistics 找到1个错误:文件:C:\\ Users \\ Cori \\ Desktop \\ Statistics.java [line:41]错误:方法bubbleSort(int [])未定义类型统计信息

Tells you there is missing method bubbleSort() with parameter int[] 告诉你缺少方法bubbleSort()参数int[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM