- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个旨在执行一些统计分析的程序。具体来说,我希望它在数组中存储一些随机整数(例如 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
时,数据数组会立即在构造函数中使用max
和min
字段进行初始化。但此时它们为零(因为您将它们留空,并且 Java 将空白 int
声明初始化为零)。然后您调用随机数生成器:
data[i] = rand.nextInt((max - min + 1) + min);
min
和 max
为零,因此计算结果为:
data[i] = rand.nextInt(1);
和 Random.nextInt()
返回最多但不包括输入的值(请参阅 docs )。
所以你的“随机”数据将始终为零;因此最小值、最大值和平均值也将为零。
关于java - (Java)统计分析作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220158/
我正在开发一个统计分析程序,它将计算很多东西。我不需要构建方面的帮助,因为我知道需要如何完成。我遇到的问题与我构建的数组有关。当我尝试显示数据时,我得到一个非常奇怪的输出。下面是输出 public c
我正在一个包含大约 100 万条记录的数据库中寻找模式。我一直在使用 Keras 和 TensorFlow,特别是 LSTM 进行一些实验, 但是,由于我是该领域的新手,因此我发现执行一些非常具体的查
我正在开发一个对用户数据执行回归分析的网络应用程序。 在后端,RoR 负责应用程序逻辑,所有统计分析均由 R 完成(因为 Ruby 的统计包很差)。 鉴于 R 和 RoR 都是单线程,并且该应用预计会
我是一名优秀的程序员,十分优秀!