简体   繁体   English

使用for循环从文本文件添加值?

[英]Adding values from a text file using a for loop?

So I'm working with two classes and a text file. 所以我正在使用两个类和一个文本文件。 In my main I have all my getters and setters, but I'm trying to find the sum of the pay, and I can't figure it out. 在我的主要工作中,我拥有所有的获取者和安置者,但是我试图找到薪水的总和,我无法弄清楚。 Here's what I have so far: 这是我到目前为止的内容:

private static void printStats(Worker[] people, int count) {

    double total = 0.0;

    for (int i = 0; i< count; i++) 
        total += people[i];
}

I don't know what I'm doing wrong. 我不知道我在做什么错。 Also, I'm just starting out so I don't have a good grasping of the concept and I'm genuinely lost. 另外,我只是刚开始,所以我对这个概念没有很好的了解,我真的迷路了。 :-/ :-/

Here's my Worker class: 这是我的工人班:

public class Worker { 公共类工作者{

private String first;
private String last;
private int total_hrs;
private double pay;

public Worker(String last, String first, int total_hrs, double pay) {

    this.first = first;
    this.last = last;
    this.total_hrs = total_hrs;
    this.pay = pay;

}

public void setFirst(String first) {

    this.first = first;

}

public void setLast(String last) {

    this.last = last;

}

public void setTotal_hrs(int total_hrs) {

    this.total_hrs = total_hrs;

}

public void setPay(double pay) {

    this.pay = pay;

}

public String getFirst() {

    return first;

}

public String getLast() {

    return last;

}

public int getTotal_hrs() {

    return total_hrs;

}

public double getPay() {

    return pay;

}
private static void printStats(Worker[] people, int count) {

    double total = 0.0;

    for (int i = 0; i< count; i++) 
        total += people[i].getTotal_hrs();
}

calling people[i] is just accessing a Worker object. 调用people[i]只是访问一个Worker对象。 Once you have the object, you need to call getTotal_hrs() to get the hours of the worker at index i in he array. 一旦有了对象,就需要调用getTotal_hrs()来获取位于数组i中索引i处的工作人员的小时数。 Or getPay() or whatever you're trying to sum, but it must be double, int, etc. 或getPay()或任何您想要求和的东西,但它必须是double,int等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM