努力计算java中的平均值,最小值和最大值

时间:2013-11-29 23:15:37

标签: java

我目前正努力计算编码中的平均值,最小值和最大值。我收到很多错误,不确定我做错了什么。对不起,如果我没有提供足够的信息,我会在需要时更新我的​​帖子。此外,如果您能解释为什么使用该代码以便我能理解,我将不胜感激。谢谢。

编辑 - 我想要它做的是当用户输入一组数字并完成输入数字时,我希望它显示用户在直方图之后输入的数字的平均值以及最大值和最小值。我假设我必须使用我的count变量以及我的num变量,但仍然在努力实现它。

我收到的错误会在我的编码中显示在编码行中。

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);
}
}

4 个答案:

答案 0 :(得分:1)

  • 如果你有一个数组,通常首先通过将初始最小值设置为已知最大值“无穷大”或在这种情况下为100来计算最小值。下一步是迭代你的值集并检查值是否低于当前最小值,如果设置为最小值则为该值。

    int min = 100;
    
    for (int i = 0; i < total; i++) {
        if (array[i] < min) {
            min = array[i];
        }
    }
    
  • 为最大值,反之,将初始最大值设置为0或负“无穷大”,并检查数组中的值是否大于当前最大值。

  • 表示平均值,将所有值相加并将结果除以数组中元素的总量。

    int sum = 0;
    
    for (int i = 0; i < total; i++) {
        sum += array[i];
    }
    
    double avg = (double) (sum) / total;
    

在java正无穷大中,整数类型表示为Integer.MAX_VALUE,负无穷大:Integer.MIN_VALUE

答案 1 :(得分:1)

假设我们有一个值数组:int [] values = {some values}

计算平均值:

  • 遍历数组并计算总和
  • 按元素数除以和

    int sum = 0;
    
    for(int i : values) {
        sum += i;
    }
    
    double average = (double)(sum) / values.length;
    

查找最小值和最大值:

  • 循环遍历数组并将当前元素与min和max进行比较并适当设置

    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;
    }
    

答案 2 :(得分:0)

我看到其他帖子已经回答了如何计算数组中的平均值,最小值和最大值,所以我只会用你的代码解决错误。

您收到错误消息int cannot be dereferenced

tmp.java:48: int cannot be dereferenced
        for (int i = 0; i < count.length; i++) {
                                 ^

这源于您将count计为int而不是数组的事实。实际上,所有的编译错误都来自这里。变量count不具有长度,也不能访问其元素(它没有)。在代码的最后部分(出现错误的地方),您将count,min和max混淆为数组,而它们实际上只是整数。你的代码很接近(有正确的想法),但我认为你可能会混淆一些语法。

答案 3 :(得分:0)

因为你的数组长度总是5000,所以我不得不使用第二个for循环来计算最小数量。否则,如果我要使用第一个for循环(现在已注释),最小数字将始终为0(如果输入少于5000个数字),因为总会有尾随元素= 0.我建议使用ArrayList代替,但我的代码使用您创建的相同数组。

我评论了我在代码中更改/添加的所有内容:

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);
}