【问题标题】:Trouble finding average and total when getting information from a .txt file从 .txt 文件中获取信息时无法找到平均值和总数
【发布时间】:2016-03-03 23:09:01
【问题描述】:

在我的面向对象编程课程中,我有一个作业要求我开发一个 java 代码,该代码在使用 .txt 文件中的信息时打印出假公司的工资信息。我能够让它打印出员工姓名和所有内容,但我在理解如何获得平均工资时遇到了一点麻烦,我已经开始了一种方法,它在下面的代码。任何帮助将不胜感激。

员工等级:

public class Employee {
    private String firstName;
    private String lastName;
    private String rank;
    private double salary;

    public Employee(String firstName, String lastName, String rank, double salary) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.rank = rank;
        this.salary = salary;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getRank() {
        return rank;
    }
    public void setRank(String rank) {
        this.rank = rank;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

prog5 类:

import java.util.*;
import java.io.*;

public class Prog5 {
    public static void main(String[] args) {

        String firstName = null;
        String lastName = null;
        String rank = null;
        double salary = 0.0;
        Scanner inFile = null;
        Employee[] arr = new Employee[99];
        int num = 0;

        try {
            inFile = new Scanner( new File ( "c:\\Users\\admin\\workspace\\Shumaker5\\src\\employees.txt"));
        }
        catch(FileNotFoundException e) {
            System.err.println(" File not found");
            System.exit(1);
        }

        //read file
        while(inFile.hasNext()) {
            try {
                firstName = inFile.next();
                lastName = inFile.next();
                rank = inFile.next();
                salary = inFile.nextDouble();
            }
            catch(InputMismatchException e) {
                System.err.println("File format error");
                System.exit(1);
            }

            Employee emp = new Employee(firstName, lastName, rank, salary);
            arr[num] = emp;
            num++;

            /**
             * method to calculate average
             * @param temperatures
             * @param counter
             * @return
             */
        /** public static double average( double[] salary, int num )
            {
                double sum = 0;
                for( int t = 0; t < num; t++)
            {
                    sum += salary[t];
            }
                return (double) sum / num; 
            } */

            System.out.println(emp);
        }   
        System.out.println();
        System.out.println("Wayne Industries");
        System.out.println();
        System.out.println("Number of Employees:    " + num);
        System.out.println("Average Salary:   ");
        System.out.println("Annual Total:    ");
        System.out.println();
        System.out.printf("%-15s %-15s %-6s %-8s \n", "Last Name", "First Name", "Rank", "Salary");
        System.out.println();

        for(int x = 0; x < num; x++) {
            System.out.printf("%-15s %-15s %-6s %-8.2f\n", arr[x].getLastName(),
                    arr[x].getFirstName(), arr[x].getRank(), arr[x].getSalary());
        }
        inFile.close();
    }
}

【问题讨论】:

  • 您能指出您遇到问题的地方吗?当你有一个特定的问题时,你应该避免只把你所有的代码...
  • 我说问题区域被注释掉了。它在 prog 类中

标签: java average


【解决方案1】:

我怀疑您需要先以字符串形式检索薪水,然后提取双精度数(例如使用 Double.parseDouble(salary))。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你很接近。我们只需要传递员工数组(而不是他们的薪水)并计算方法中的平均值。下面提供了一个示例:

    public static double average( Employee[] employees ){
            double sum = 0;
            for( int t = 0; t < employees.length; t++){
                sum += employee[t].getSalary();
            }
            return (double) sum / employees.length; 
        }
    

    【讨论】:

    • 非常感谢。这是我第一次使用文本文件,所以我有点迷茫,这对 immensley 有帮助
    • 更新后的代码仍然出现两个错误,在返回语句中,我得到 void 方法无法返回值。在公共静态双线上,它说变量平均值的非法修饰符;只允许决赛
    • 您可能将方法粘贴到错误的位置。您能否确保该方法没有被粘贴到现有方法中?
    • 为了确定,我把它移到了许多不同的地方,但无论我把它们放在哪里,错误都是一样的。
    【解决方案3】:

    您已经定义了 Employee 类。 该员工包含您需要的所有信息,并且您已经为每个员工捕获了所有信息。

    如何计算平均工资?

    1. 根据另一个响应的建议,您可以更改平均函数的签名,将员工数组作为输入参数传递。然后在注释代码中(用于计算平均工资),将“salary[t]”替换为employee[t].getSalary()。
    2. 另一种方法是在您逐行读取文件并将其信息存储在 Employee 类对象中时跟踪工资总和和员工总数。使用这种方法,您无需再遍历数组一次。

    您可能需要考虑的一些极端情况是:

    • 如果文件为空怎么办?您可能想添加一个 if 条件来防止除以零?
    • 您打算如何处理溢出?例如,如果工资总和不能变成整数值?一些建议是:
      • 使用 unsigned int 而不是 int 作为 sum 变量(或 unsigned long int)。这是因为没有负工资,您可以覆盖更大范围的数字。

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 1970-01-01
      • 2018-09-15
      • 2019-05-02
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      相关资源
      最近更新 更多