Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么数组的平均值不正确?_Java_Arrays - Fatal编程技术网

Java 为什么数组的平均值不正确?

Java 为什么数组的平均值不正确?,java,arrays,Java,Arrays,我仍然在学习java,到目前为止,我已经完成了我的任务,没有遇到任何问题。当我计算数组中值的平均值时,我总是得到错误的答案。以下是我获取值的代码: public static int [] inputGrades() { Scanner kb = new Scanner (System.in); int [] iGrades = new int [10]; System.out.print("\nInput test scores, ent

我仍然在学习java,到目前为止,我已经完成了我的任务,没有遇到任何问题。当我计算数组中值的平均值时,我总是得到错误的答案。以下是我获取值的代码:

public static int [] inputGrades()
    {
        Scanner kb = new Scanner (System.in);
        int [] iGrades = new int [10];
        System.out.print("\nInput test scores, enter -1 when you're finished.\n");
        for (int i =0; i<iGrades.length;i++)
        {
            iGrades[i]=kb.nextInt();
            if (iGrades[i] ==-1)
            {
                break;
            }
    }
        return iGrades;   
public static int[]inputGrades()
{
扫描仪kb=新扫描仪(System.in);
int[]iGrades=新的int[10];
System.out.print(“\n输入测试分数,完成后输入-1。\n”);

对于(int i=0;i查看array.length。它是10,因此
dAverage=iSum/array.length
总是除以10。

有两个问题:

(1) 这是整数(即截断)除法:

dAverage = iSum / array.length;
你想要

dAverage = iSum / (double)array.length`;
(2)
数组。长度始终为10,并且在数组末尾包含零。因此,在您的示例中,您实际计算的是10、20、30、40、50、0、0、0、0的平均值,实际上是15

您需要记录用户实际输入的数字,然后除以该数字


更好的方法是使用
ArrayList
而不是
int[]
,这样您就可以完全避开这个问题。

您好,这里有一个不使用数组的解决方案,在这之后,我将使用ArrayList发布解决方案,这样您就可以了解它的工作原理:)。我希望它能帮助您

    int total = 0;
    int numberOfPeople = 0;
    int number;

    Scanner input = new Scanner(System.in);
    ArrayList<Integer> myList = new ArrayList<Integer>(5);

    System.out.println("Enter a number or -1 to finish");
    number = input.nextInt();
    while (number !=-1) {

        total = total + number;
        numberOfPeople++;
        System.out.println("Enter a number or -1 to finish");
        number = input.nextInt();

    }

    System.out.println("The average is: " + total / numberOfPeople);
int-total=0;
int numberOfPeople=0;
整数;
扫描仪输入=新扫描仪(System.in);
ArrayList myList=新的ArrayList(5);
System.out.println(“输入一个数字或-1完成”);
number=input.nextInt();
while(数字!=-1){
总数=总数+数量;
numberOfPeople++;
System.out.println(“输入一个数字或-1完成”);
number=input.nextInt();
}
System.out.println(“平均值为:“+总人数/人数”);

这是我使用ArrayList的另一个解决方案。希望它能帮助您:)

int number=0;
int-total=0;
int numberOfPeople=0;
ArrayList myList=新的ArrayList(5);
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个数字或-1完成”);
number=input.nextInt();
while(数字!=-1){
总数=总数+数量;
numberOfPeople++;
if(numberOfPeople==5){
打破
}
System.out.println(“输入一个数字或-1完成”);
number=input.nextInt();
myList.add(编号);
}
System.out.println(“平均值为:“+总人数/人数”);

我还没有看过
ArrayList
呢,还有什么方法可以去掉未使用的插槽吗?如果你还不应该使用列表API,请声明你的数组“足够大”并将其视为必须管理的已分配内存块。在注释中将其声明为依赖项。然后,跟踪单独收集的值的数量,而不是使用length属性。@jdv谢谢,我创建了另一个变量
iCount
    int total = 0;
    int numberOfPeople = 0;
    int number;

    Scanner input = new Scanner(System.in);
    ArrayList<Integer> myList = new ArrayList<Integer>(5);

    System.out.println("Enter a number or -1 to finish");
    number = input.nextInt();
    while (number !=-1) {

        total = total + number;
        numberOfPeople++;
        System.out.println("Enter a number or -1 to finish");
        number = input.nextInt();

    }

    System.out.println("The average is: " + total / numberOfPeople);
    int number = 0;
    int  total = 0;
    int numberOfPeople = 0;
    ArrayList<Integer> myList = new ArrayList<Integer>(5);
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a number or -1 to finish");
    number = input.nextInt();
       while (number !=-1 ) {

        total = total + number;
        numberOfPeople++;
        if(numberOfPeople == 5){
            break;
        }

        System.out.println("Enter a number or -1 to finish");
        number = input.nextInt();
        myList.add(number);

    }
       System.out.println("The average is: "+ total/numberOfPeople);