为什么我的累加器没有增加,而且始终返回零?

huangapple go评论117阅读模式
英文:

why is my accumulator not incrementing and always returuns zero?

问题

我刚开始学习Java所以我的问题可能对大多数人来说很有趣
我试图编写一个计算课程数组平均值的程序但出于某种原因我的累加器没有增加

public void setStudents() {
    accumulator = 0;
    
    for (int x = 0; x < course.length; x++) {
        accumulator = accumulator + course[x].result;
    }
}

public double getAverage() {
    average = (double) (accumulator / course.length);
    return average;
}

public int getTotal() {
    return accumulator;
}
英文:

I have just started to learn java so my question might be funny to most of you,
i am trying to write a program that calculate an average for an array of courses
and for some reason my accumulator is not incrementing

public void setStudents() {
	
		accumulator=0;
		
	for(int x=0;x&lt;course.length;x++)
	{
		 accumulator =accumulator+ course[x].result;
		
	}
}
public double getAverage () {
   average=(double)(accumulator/course.length);
	return average;
}
public int getTotal() {
	return accumulator;
}

答案1

得分: 1

我假设这段代码被包裹在一个带有名为accumulator的成员字段变量的类中。在这种情况下,当类被构造时,应该调用setStudents方法,以便正确设置累加器。如果你在构造函数中没有调用setStudents方法,或者在调用getAverage方法之前没有任何地方调用过,那么accumulator字段将被设置为其默认值,即0。这可能是你得到0作为结果的原因。这可以通过在该类的构造函数中调用setStudents,或者在调用getAverage方法之前的任何地方调用来修复。另外,在getAverage方法中,在进行算术运算之前,你应该先将其转换为double类型,否则仍然会调用整数除法,然后再转换为double类型。因此,这行代码
average=(double)(accumulator/course.length);
应改为
average=(double)(accumulator)/course.length;

英文:

I'm assuming this is wrapped in a class with a member field variable called accumulator. In this case, when the class is constructed, the setStudents method should be called, so that the accumulator is properly set. If you don't call the setStudents method in the constructor, or anywhere before you call the getAverage method, the accumulator field will just be set to its default value, which is 0. This is probably the reason why you are getting 0 as the result. This can be fixed by calling setStudents in the constructor of this class, or anywhere before you call the getAverage method. Also, in the getAverage method, you should be casting to double before doing the arithmetic, or else the integer division still gets called and then gets cast to a double afterward. So the line

average=(double)(accumulator/course.length);

should be

average=(double)(accumulator)/course.length;

答案2

得分: 0

class Mine {
    int accumulator;
    double average;
    Course[] course = {Course.builder().result(10).build(), Course.builder().result(20).build(), Course.builder().result(30).build()};

    public static void main(String[] args) {
        Mine obj=new Mine();
        obj.setStudents();
        double avg = obj.getAverage();
        System.out.println(avg);
    }

    public void setStudents() {
        Course[] course = {Course.builder().result(10).build(), Course.builder().result(20).build(), Course.builder().result(30).build()};
        for (int x = 0; x < course.length; x++) {
            accumulator = accumulator + course[x].result;
        }
    }

    public double getAverage() {
        average = (double) (accumulator / course.length);
        return average;
    }

    public int getTotal() {
        return accumulator;
    }
}

@Builder
@Data
class Course {
    int result;
}
英文:
class Mine {
    int accumulator;
    double average;
    Course[] course = {Course.builder().result(10).build(), Course.builder().result(20).build(), Course.builder().result(30).build()};

    public static void main(String[] args) {
        Mine obj=new Mine();
        obj.setStudents();
        double avg = obj.getAverage();
        System.out.println(avg);
    }

    public void setStudents() {
        Course[] course = {Course.builder().result(10).build(), Course.builder().result(20).build(), Course.builder().result(30).build()};
        for (int x = 0; x &lt; course.length; x++) {
            accumulator = accumulator + course[x].result;
        }
    }

    public double getAverage() {
        average = (double) (accumulator / course.length);
        return average;
    }

    public int getTotal() {
        return accumulator;
    }
}

@Builder
@Data
class Course {
    int result;
}

If you see above code, i had just modified your existing code.

You made some minor mistake with getting and assigning local variables.

Just go through with this link, which will guide you about scope,global variable, local variables

huangapple
  • 本文由 发表于 2020年8月22日 02:31:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定