【问题标题】:(Java) Statistical analysis assignment(Java) 统计分析作业
【发布时间】:2015-05-27 00:40:14
【问题描述】:

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

我现在最大的问题是主程序需要两个输入来执行任何方法(放入第一个后什么都不做),而且每个方法都返回 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!");
    }

}

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

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

提前致谢!

【问题讨论】:

  • 您好 Suraj,我想您已经意识到,您的问题有点宽泛。如果您发布的程序不完整,很难给出具体建议。你的意思是你还没有全部发布,或者你还没有写完?
  • 我还没写完呢。似乎快完成了,但我想在继续之前解决返回和双重输入问题。

标签: java arrays loops statistics


【解决方案1】:

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

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

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

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

Random.nextInt() 返回的值不超过输入值,但不包括输入值(参见docs)。

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

【讨论】:

  • 感谢您的回复:设置最小值和最大值似乎已经奏效,我现在正在获取方法 1-6 的值。还发现我有额外的 nextInt 行导致双输入问题,因此已解决。现在只需要弄清楚如何在列中显示集合,计算与输入参数匹配的数字......我就完成了。
猜你喜欢
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2018-07-03
  • 2011-02-22
  • 1970-01-01
  • 2012-03-29
相关资源
最近更新 更多