The full assignment was to create a number of methods, e.g. getMode and getAverage, to compute stats for an array with randomly-generated numbers and user-inputted ceiling max, floor min, and sample size. All of my other methods seem to work, but every time I try returning the populated set's min value, I get 0--even if the inputted min was something higher.
Here's my code:
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;
...
And the main program:
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();
}
...
Any idea why my program isn't returning appropriate min values (equal to or above what the user inputs)? The max seems to come out fine--sometimes it's below the user's inputted max, and most often it's equal. I've looked at other questions re. min/max and arrays with random numbers, but haven't been able to apply their solutions to my own problem.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire