在java中努力计算平均值、最小值和最大值
我目前正在努力计算编码中的平均值、最小值和最大值。我收到了很多错误,不确定我做错了什么。对不起,如果我没有提供足够的信息,我会更新我的帖子,如果需要的话。另外,如果您能解释一下为什么使用该代码,我将不胜感激,以便我能理解。多谢各位 编辑-我希望它做的是当用户输入一组数字并完成数字输入时,我希望它显示用户在直方图之后输入的数字的平均值以及最大值和最小值。我假设我将不得不使用count变量和num变量,但在实现它时仍然很困难 我收到的错误显示在下面的编码行中在java中努力计算平均值、最小值和最大值,java,Java,我目前正在努力计算编码中的平均值、最小值和最大值。我收到了很多错误,不确定我做错了什么。对不起,如果我没有提供足够的信息,我会更新我的帖子,如果需要的话。另外,如果您能解释一下为什么使用该代码,我将不胜感激,以便我能理解。多谢各位 编辑-我希望它做的是当用户输入一组数字并完成数字输入时,我希望它显示用户在直方图之后输入的数字的平均值以及最大值和最小值。我假设我将不得不使用count变量和num变量,但在实现它时仍然很困难 我收到的错误显示在下面的编码行中 import java.util.Sca
import java.util.Scanner;
public class Histogram1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[5000];
int num = 0;
int count = 0;
int total = 1;
int max = 0;
int min = 0;
System.out.println ("Enter students marks in the range 0 to 100\n");
loop: for (count = 0; count <= total; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
if (num < 0 || num > 100)
{
break loop;
}
array[count] = num;
total = count+1;
}
System.out.println ("How many times a number between 0-100 occur.");
String[] asterisk = {"0- 29 | ", "30- 39 | ","40- 69 | ", "70- 100 | "}; //4 strings
for (count = 1; count <= total; count++)
{
num=array[count];
if (num >=0 && num <=29) asterisk [0] +="*";
else if (num>29 && num <=39) asterisk[1] +="*";
else if (num>39 && num <=69) asterisk[2] +="*";
else if (num>69 && num <=100) asterisk[3] +="*";
}
for (count =0;count < 4;count++)
System.out.println(asterisk[count]);
System.out.println("The total amount of students is " + total);
**int cannot be dereferenced** for (int i = 0; i < count.length; i++) {
**array required, but int found** num += count[i];
**array required, but int found** if (min > num[i]) {
**array required, but int found** min = num[i];
}
**array required, but int found** if (max < num[i]) {
**array required, but int found** max = num[i];
}
}
**int cannot be dereferenced** double average = (double) num / count.length;
System.out.printf(" min: " + min);
System.out.printf("%n max: " + max);
System.out.printf("%naverage: %.1f", average);
}
}
import java.util.Scanner;
公共类历史图1{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int[]数组=新int[5000];
int num=0;
整数计数=0;
整数合计=1;
int max=0;
int min=0;
System.out.println(“输入0到100范围内的学生分数\n”);
循环:for(计数=0;计数100)
{
断环;
}
数组[计数]=num;
总数=计数+1;
}
System.out.println(“0-100之间的数字出现的次数”);
字符串[]星号={“0-29 |”,“30-39 |”,“40-69 |”,“70-100 |”};//4个字符串
对于(计数=1;计数=0&&num 29&&num 39&&num 69&&num num[i]){
**需要数组,但找到int**min=num[i];
}
**需要数组,但找到int**if(max
- 如果您有一个数组,通常通过首先将初始最小值设置为已知的最大值“无穷大”或在本例中为100来计算最小值。下一步是迭代您的值集,检查某个值是否低于当前最小值,如果低于,则将最小值设置为该值
int min = 100; for (int i = 0; i < total; i++) { if (array[i] < min) { min = array[i]; } }
int最小值=100; 对于(int i=0;i
- 对于“最大值”,请使用另一种方法,将初始最大值设置为0或负“无穷大”,并检查数组中的值是否大于当前最大值
- 对于“平均值”,求和所有值并将结果除以数组中的元素总数
int sum = 0; for (int i = 0; i < total; i++) { sum += array[i]; } double avg = (double) (sum) / total;
int和=0; 对于(int i=0;i
在java中,正无穷大作为整数类型表示为
integer.MAX_VALUE
和负无穷大:integer.MIN_VALUE
让我们假设有一个值数组:int[]VALUE={some VALUE}
要计算平均值:
- 循环遍历数组并计算和
- 将总和除以元素数
int sum = 0; for(int i : values) { sum += i; } double average = (double)(sum) / values.length;
- 循环遍历数组,将当前元素与最小值和最大值进行比较,并适当设置它们
int max = -2147483648; //set to min int value, -2^31 int min = 2147483647; //set to max int value, 2^31 - 1 for (int i : values) { if (i > max) max = i; if (i < min) min = i; }
int max=-2147483648//设置为最小整数值,-2^31 int min=2147483647//设置为最大整数值,2^31-1 for(int i:值){ 如果(i>max)max=i; 如果(i
int不能被取消引用
:
tmp.java:48: int cannot be dereferenced
for (int i = 0; i < count.length; i++) {
^
tmp.java:48:int无法取消引用
for(int i=0;i
这是因为您将count定义为
int
而不是数组。事实上,所有编译错误都来自这里。变量count
将没有长度,您也无法访问其元素(它没有)。在代码的最后一部分(出现错误的地方)您将count、min和max混淆为数组,而实际上它们只是整数。您的代码非常接近(有正确的想法),但我认为您可能混淆了一些语法。因为您的数组长度始终为5000,所以我必须使用第二个for循环来计算最小数。否则,如果我使用第一个for循环(现在已注释),最小数将始终为0(如果输入的数字少于5000个)因为后面总是有=0的元素。我建议改为使用,但我的代码使用与您创建的数组相同的数组
我对您代码中更改/添加的所有内容进行了注释:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[5000];
int num = 0;
int count = 0;
int total = 0; // start this at 0, in case no inputs are valid
double sum = 0; // make this a double to get a decimal average
int max = 0; // start at the lowest possible number for max
int min = 100; // start at the highest possible number for min
double average = 0;
System.out.println ("Enter students marks in the range 0 to 100\n");
loop: for (count = 0; count <= total; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
if (num < 0 || num > 100 || total == 5000) // can't enter more than 5000 (array length restriction)
{
break loop;
}
array[count] = num;
sum += num; // keep track of the sum during input
total = count+1;
}
System.out.println ("How many times a number between 0-100 occur.");
String[] asterisk = {"0- 29 | ", "30- 39 | ","40- 69 | ", "70- 100 | "}; //4 strings
for (count = 1; count <= total; count++)
{
num=array[count];
if (num >=0 && num <=29) asterisk [0] +="*";
else if (num>29 && num <=39) asterisk[1] +="*";
else if (num>39 && num <=69) asterisk[2] +="*";
else if (num>69 && num <=100) asterisk[3] +="*";
}
for (count =0;count < 4;count++)
System.out.println(asterisk[count]);
System.out.println("The total amount of students is " + total);
// calculate the average
average = sum / total;
// calculate the min and max
// use this for loop if the length of the array is
// the amount of the inputs from the user
/*for (int i : array) // for every int in the array of inputted ints
{
if (i > max) max = i; // if this int in array is > the last recording max number, set the new max number to be this int
if (i < min) min = i; // if this int in array is < the last recording min number, set the new min number to be this int
}*/
for (int i = 0; i < total; i++) // for every inputted int ( < total so that you exclude the trailing elements that are = 0)
{
if (array[i] > max) max = array[i]; // if this int in array is > the last recording max number, set the new max number to be this int
if (array[i] < min) min = array[i]; // if this int in array is < the last recording min number, set the new min number to be this int
}
// my way of printing results
//System.out.println("\n Average: " + average);
//System.out.println("Max: " + max + "\n Min: " + min);
System.out.printf(" min: " + min);
System.out.printf("%n max: " + max);
System.out.printf("%naverage: %.1f", average);
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int[]数组=新int[5000];
int num=0;
整数计数=0;
int total=0;//如果没有有效的输入,则从0开始
double sum=0;//将其设为double以获得十进制平均值
int max=0;//从max的最低可能数字开始
int min=100;//从min的最大可能数字开始
双平均=0;
System.out.println(“输入0到100范围内的学生分数\n”);
循环:for(count=0;count 100 | | total==5000)//不能输入超过5000(数组长度限制)
{
断环;
}
数组[计数]=num;
sum+=num;//在输入期间跟踪总和
总数=计数+1;
}
System.out.println(“0-100之间的数字出现的次数”);
字符串[]星号={“0-29 |”,“30-39 |”,“40-69 |”,“70-100 |”};//4个字符串
对于(计数=1;计数=0&&num 29&&num 39&&num 69&&num