java - (Java)统计分析作业

标签 java arrays loops statistics

我目前正在开发一个旨在执行一些统计分析的程序。具体来说,我希望它在数组中存储一些随机整数(例如 10,介于最小值和最大值之间),通过单独的方法计算最小值、最大值、模式和其他一些值,并为用户提供一个菜单,用于选择一种方法(如果这样做的话,则循环回到菜单)或退出。

我现在最大的问题是主程序需要两个输入来执行任何方法(输入第一个后不执行任何操作),而且每个方法都返回 0 或 0.0。

这是我的代码:

import java.util.Random;

public class Stats extends Main
{
    int sampleSize;
    double count;
    double ave;
    double sum;
    int min;
    int max;
    int mode;
    int evenCount;
    int oddCount;
    int countMatching;

    //Constructor: use an RNG to generate sampleSize integers between minValue and maxValue. Store the numbers in an array named 'data'.
    public Stats()
    {
        sampleSize = 10;
        data = new int[sampleSize];

        for (int i = 0; i < sampleSize; i++)
            {
                Random rand = new Random();
                data[i] = rand.nextInt((max - min + 1) + min);
            }
        return;
    }
    //Method: return the sample set's max value
    public int getMax()
    {
        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()
    {
        min = data[0];
        for(int i = 0; i < sampleSize; i++)
        {
            if (data[i] < min)
                min = data[i];
        }
        return min;
    }
    //Method: return the average value
    public double getAve()
    {
        count = sampleSize;
        sum = 0;
        for(int i = 0; i < sampleSize; i++)
        {
            sum = sum + data[i];
        }
        ave = sum / count;
        return ave;
    }
    //Method: return the mode; in case of a tie, choose the smallest value
    public int getMode()
    {
        int popularity1 = 0;
        int popularity2 = 0;
        int array_item;
        for(int i = 0; i < sampleSize; i++)
        {
             array_item = data[i];
             for(int j = 0; j < sampleSize; j++)
             {
                if(array_item == data[j])
                     popularity1++;
             }
                if(popularity1 >= popularity2)
                  {
                  mode = array_item;
                  popularity2 = popularity1;
                  }
        }
        return mode;
    }
    //Method: return the count of even numbers
    public int getEven()
    {
        int evenCount = 0;
        for (int i = 0; i < sampleSize; i++)
        {
            if (data[i] % 2 == 0)
                evenCount++;
        }
        return evenCount;
    }
    //Method: return the count of odd numbers
    public int getOdd()
    {
        int oddCount = 0;
        for (int i = 0; i < sampleSize; i++)
        {
            if (data[i] % 2 != 0)
                oddCount++;
        }
        return oddCount;
    }
    //Display all numbers, formatted in columns (hint: pg. 158)
    public void displaySampleSet()
    {
        for (int i = 0; i < sampleSize; i++)
        {

        }
    }
    //Return the count of numbers in the sample set that match the input parameter
    public int countMatching(int match)
    {
        int countMatching = 0;
        return match;
    }
    //Create a list of private variable(s) that belong to the Stats class
    private int[] data;

}

这是主程序:

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        int input;
        int stats;
        Scanner keyboard = new Scanner(System.in);

        Stats g = new Stats();
        System.out.println("Welcome to the Stats Program!");
        System.out.println();
        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();
        while (stats != 9)
        {
            if (stats == 1)
            {
                g.getMax();
                input=keyboard.nextInt();
                System.out.println("Max is: " + g.getMax());
            }
            else if (stats == 2)
            {
                g.getMin();
                input=keyboard.nextInt();
                System.out.println("Min is: " + g.getMin());
            }
            else if (stats == 3)
            {
                g.getAve();
                input=keyboard.nextInt();
                System.out.println("Mean is: " + g.getAve());
            }
            else if (stats == 4)
            {
                g.getMode();
                input=keyboard.nextInt();
                System.out.println("Mode is: " +g.getMode());
            }
            else if (stats == 5)
            {
                g.getEven();
            }
            else if (stats == 6)
            {
                g.getOdd();
            }
            else if (stats == 7)
            {
                g.displaySampleSet();
            }
            else if (stats == 8)

        System.out.println("");
        System.out.println("View other stats?");
        System.out.println("");
        System.out.println("1) Max 2) Min 3) Mean 4) Mode 5) Count of evens 6) Count of odds 7) Sample set numbers 8) Count of numbers that match input parameter 9) Exit");
        stats = keyboard.nextInt();
        }
        System.out.println("Thank you for using the Stats Program. See you next time!");
    }

}

两个程序都不完整,但我仍然获取每个方法的值(在两次输入之后),执行后循环,并且退出按预期工作。

有任何明显错误/缺失的提示或部分吗?我真的很想了解这一点。

提前致谢!

最佳答案

当您创建Stats时,数据数组会立即在构造函数中使用maxmin字段进行初始化。但此时它们为零(因为您将它们留空,并且 Java 将空白 int 声明初始化为零)。然后您调用随机数生成器:

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

minmax 为零,因此计算结果为:

data[i] = rand.nextInt(1);

Random.nextInt() 返回最多但不包括输入的值(请参阅 docs )。

所以你的“随机”数据将始终为零;因此最小值、最大值和平均值也将为零。

关于java - (Java)统计分析作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220158/

相关文章:

php - 如何将数组值的数组组合成一个数组?

c - 初始化全局数组的所有元素时的垃圾值

python - 这个 for 循环生成有序非重复组合列表的错误在哪里?

java - 如何从java数组列表中删除用户定义的对象

java - 批量解压.gz文件

java - 提示输入数组大小

java - 执行以下代码片段所花费的时间差异很大的原因是什么?

r - 遍历列并将字符串长度添加为新列

java - 使用 LocationServices 获取纬度和经度

java - JOOQ 多个配置文件