【问题标题】:How to calculate average of array input correctly?如何正确计算数组输入的平均值?
【发布时间】:2019-11-09 12:46:21
【问题描述】:

当我输入任何数量的数字时,它给了我错误的平均值我不知道为什么我尝试了很多方法但平均值仍然无法正常工作 我做错了什么得到错误的结果? 任何建议..

import java.util.Scanner;
public class Assignment_Help2 {
    public static void main(String[] args){
        Scanner keyboard = new Scanner (System.in);

        String[] department = new String[5];
        int[] employeesno = new int[5];
        String [] address = new String[5];

        int index;
        index = 0;
        int totalentered = 0;
        String temp;

        // One loop to enter all information
        //INPUT LOOP
        for (index = 0; index < 5; index++)
        {
                System.out.print("Enter a department  or stop ..: ");
                department[index] = keyboard.nextLine();
                if (department[index].equals("stop"))
                {
                    break;

                }
                System.out.print("employees_no: ");
                temp = keyboard.nextLine();
                // convert string to a number & then store it as integer in the array
                employeesno[index] = Integer.valueOf(temp);

                System.out.print("Enter address: ");
                address[index] = keyboard.nextLine();   
                totalentered++;

        } //END OF FOR LOOP

        System.out.println("==================================");   

        System.out.println(String.format("%-20s", "department")+
                String.format("%-18s", "no_employees")+ 
                String.format("%-20s", "Address"));



        // Another separate loop to display the information stored in arrays
        //OUTPUT LOOP   
        for (index = 0; index < totalentered; index++)
        {

            System.out.println(String.format("%-20s", department[index]) + 
                     String.format("%-18d", employeesno[index])+ 
                     String.format("%-20s", address[index]));    

        } 

        int sum1=0;
        double average;
        for(int l=0; l <employeesno.length  ; l++) 
         {
           sum1 += employeesno[l];
         } average= sum1/employeesno.length;
         System.out.println("aver= "+average);
         keyboard.close();      
    }
}

【问题讨论】:

  • 也许您还没有,但是当涉及到存储未知数量的元素时,您通常使用 List 实例而不是 Java 中的低级数组。你会使用一个类Department 和属性nameemployeesaddress,所以你可以只使用一个列表。
  • 我是学生,刚刚开始使用 java,它是我作业的一部分,我们已经完成了学习列表,但我尝试创建新方法并将其命名为 average 仍然给我不同的结果。感谢您的回复
  • 这是因为尽管列表包含 size() 方法,但您的计数方式不同。但是,您忘记了循环中计算平均值(以及随后的除法)的变量。

标签: java arrays input average


【解决方案1】:

sum1 是一个intemployeesno.lengthint,所以 sum1 / employeesno.length 是通过整数除法计算的。写

average = ((double) sum1) / employeesno.length`;

改为在doubles进行除法

【讨论】:

  • 员工人数有多大?也许你试图将它们总结为int 时会溢出。尝试将sum 更改为long
  • 如果你的意思是这样,我没有对员工施加任何限制
  • 是的。因此,如果您将 900000001、900000002、900000003、900000004 和 900000005 作为员工编号,将它们相加很容易溢出int 的容量。为sum1 使用long 可以解决这个问题。
猜你喜欢
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
相关资源
最近更新 更多