繁体   English   中英

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

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

所以我正在使用两个类和一个文本文件。 在我的主要工作中,我拥有所有的获取者和安置者,但是我试图找到薪水的总和,我无法弄清楚。 这是我到目前为止的内容:

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

    double total = 0.0;

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

我不知道我在做什么错。 另外,我只是刚开始,所以我对这个概念没有很好的了解,我真的迷路了。 :-/

这是我的工人班:

公共类工作者{

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

调用people[i]只是访问一个Worker对象。 一旦有了对象,就需要调用getTotal_hrs()来获取位于数组i中索引i处的工作人员的小时数。 或getPay()或任何您想要求和的东西,但它必须是double,int等。

暂无
暂无

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

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