Java:具有随机整数和输入计数的数组的最小方法

Java: min method for array with random ints and inputted count

完整的任务是创建一些方法,例如getMode 和 getAverage,用于计算具有随机生成的数字和用户输入的上限最大值、下限最小值和样本大小的数组的统计信息。我的所有其他方法似乎都有效,但每次我尝试返回 填充集 的最小值时,我得到 0——即使输入的最小值更高。

这是我的代码:

import java.util.Random;

public class Stats extends Main
{
int sampleSize;
double count;
double ave;
double sum;
int mode;
int evenCount;
int oddCount;
int countMatching;
int counter;
int target;
int match;

//Method: return the sample set's max value
public int getMax(int sampleSize, int[] data)
{
    int max = Integer.MAX_VALUE;
    max = data[0];
    for(int i = 0; i < sampleSize; i++)
    {
        if (data[i] > max)
            max = data[i];
    }
    return max;
}
//Method: return the min value
public int getMin(int sampleSize, int[] data)
{
    int min = Integer.MIN_VALUE;
    min = data[0];
    for(int i = 0; i < sampleSize; i++)
    {
        if (data[i] < min)
            min = data[i];
    }
    return min;
...

和主程序:

import java.util.Random;
import java.util.Scanner;

public class Main 
{
public static void main(String[] args) 
{
    int stats;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Welcome to the Stats Program!");
    System.out.println();
    System.out.println("Enter sample size: ");
    int sampleSize = keyboard.nextInt();
    System.out.println();
    System.out.println("What is the sample set's minimum? ");
    int min = keyboard.nextInt();
    System.out.println();
    System.out.println("What is the sample set's maximum? ");
    int max = keyboard.nextInt();
    System.out.println();

    Stats g = new Stats();
    System.out.println("Main Menu");
    System.out.println();
    System.out.println("1) Get max value");
    System.out.println("2) Get min value");
    System.out.println("3) Get the mean");
    System.out.println("4) Get the mode");
    System.out.println("5) Get the count of even numbers");
    System.out.println("6) Get the count of odd numbers");
    System.out.println("7) Display the sample set");
    System.out.println("8) Return the count of numbers in the sample set  that match the input parameter");
    System.out.println("9) Exit");
    System.out.println();
    stats = keyboard.nextInt();

    //Constructor: use an RNG to generate sampleSize integers between        minValue and maxValue. Store the numbers in an array named 'data'.
    int[] data = new int[sampleSize];
    for (int i = 0; i < sampleSize; i++)
        {
            Random rand = new Random();
            data[i] = rand.nextInt((max - min + 1) + min);
        }

    while (stats != 9)
    {
        if (stats == 1)
        {
            g.getMax(sampleSize, data);
            System.out.println("Max is: " + g.getMax(sampleSize, data));
            System.out.println();
        }
        else if (stats == 2)
        {
            g.getMin(sampleSize, data);
            System.out.println("Min is: " + g.getMin(sampleSize, data));
            System.out.println();
        }
        ...

知道为什么我的程序没有返回适当的最小值(等于或高于用户输入的值)吗?最大值似乎很好——有时它低于用户输入的最大值,而且通常是相等的。我已经看过其他问题。 min/max 和带有随机数的数组,但无法将他们的解决方案应用于我自己的问题。

提前致谢!

你的min方法似乎很有效,可能你的数组中没有小于0的数字。

这个答案并不完全是为了使您的实际方法有效,而只是一个更简单的方法的建议。

您可以使用 CollectionsCommons Lang 来查找数组的 min/max 并将原始数组转换为列表。

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;

public class tst {
    public static void main(String[] args) {
        int[] arr = {1, 23, 43, 2, 4, 5, 5, 1, 2, 3};

        List lst = Arrays.asList(ArrayUtils.toObject(arr));

        System.out.println(Collections.min(lst));
        System.out.println(Collections.max(lst));
    }
}

这将输出 min:1max:43

如果您不被迫使用原始数组并自己做所有事情,这是一个很好的解决方案。

希望对您有所帮助:)

请更改随机 nextInt 函数以获取随机值。 应该是

data[i] = rand.nextInt(max - min + 1) + min;

不是data[i] = rand.nextInt((max - min + 1) + min);

查看您的这部分代码:

for (int i = 0; i < sampleSize; i++)
    {
        Random rand = new Random();
        data[i] = rand.nextInt((max - min + 1) + min);
    }

第一个错误是你在每次迭代中创建了一个新的随机数生成器。您应该在循环之前创建它并在循环中使用它。

但这是次要的问题。更重要的是你运行rand.nextInt()的方式。假设你的 min 是 3 而你的 max 是 7。然后 (max - min + 1) + min 给你 8。这意味着你正在调用 rand.nextInt(8),这会给你一个数字,例如 0 ≤ 数 < 8.

方法 Random.nextInt(int n) 返回一个数字,满足 0 ≤ 数字 < n。所以如果你想要一个 3 ≤ number < 8 的数字,你需要它给你 0 到 5 之间的值,然后将其加到 3。这意味着:

rand.nextInt(max - min + 1) + min;

它看起来 几乎 和您写的一样,但是额外的一对括号使一切变得不同。你不能随便在任何地方加上括号。方法名称后的第一个括号定义了该方法的参数,在您的情况下,它导致 + min 成为 nextInt.

参数的一部分

还有一件事:你的 class 应该 而不是 扩展 Main。他们没有任何关系,你的 Stats 不是 Main 的一种,对吧?