简体   繁体   中英

Java Method returns initialize variable, instead of final value of variable.

Well, I'm new in java, and i'm making a method which objective is to return the average grades of a student, but instead of returning the final value I'm getting the initialize value of the variable that is returning the method.

This is the method:

private int promedio;
private int average;

StudentInfo() {
    this.average = 0; ///THIS IS WHAT THE METHOD IS RETURNING!!!
    this.totalPercent = 0;
}

public int finalAverage() {
    for (int i = 0, n = quantity-1; i <= n; i++){
        average = average + (percentage[i]/100)*(notes[i]) ;  ///THIS IS WHAT IT SHOULD RETURN!!!                  
    }
    return average;
}  

 System.out.println("El estudiante " + nombre + " cuenta con un promedio de " + myObj.finalAverage() + "." );

    if (myObj.finalAverage() < 3){
        System.out.println("El estudiante perdio la materia.");            
    }else{
        System.out.println("¡FELICIDADES! Ha pasado la materia.");  

Thank you very much for your help, and have nice day. :D

Percentage is probably an int. Dividing by 100, an int too, uses integer division . 99/100 == 0! Divide by 100 at the end, or use double (less nice).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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