user2968861
user2968861

Reputation: 495

Java average of series of numbers

public int CalculateResult(){
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks = totalMarks + grade;
    }
    return totalMarks / 5;
    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
}

I'm trying to write a program that allows the user to enter the grades for a series of students, but I'm continuously getting errors with the line return totalMarks / 5 (To get the average of 5 results).

I've tried moving the return statement to within the for loop, but the compiler still won't recognize what totalMarks is.

Upvotes: 0

Views: 102

Answers (4)

Affe
Affe

Reputation: 53

As mentioned, look out for the focus of variables and the return statement. Also note, that using the divide operator on integers floors your result, eg:

int i = 5;
int j = 10;
double d = i / j;
System.out.println(d);

This will print 0.0, since i / j equals 0.5 which is rounded down to 0.

Upvotes: 0

James Waddington
James Waddington

Reputation: 2905

It's because the scope of totalMarks is inside the for loop where you define it. To use it in the division, you need to define it outside:

public int CalculateResult(){
    int totalMarks = 0;
    int requiredGrade = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return totalMarks / 5;
}

Edit - you also should move the return to the end as per @kurochenko edit - otherwise your output is unreachable.

Upvotes: 1

kurochenko
kurochenko

Reputation: 1254

Java has block variable visibility. You have to define variable in same or parent block (block is defined by {} braces) in which you use that variable. So your code should be like this:

public int CalculateResult(){
    int requiredGrade = 0;
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        totalMarks = totalMarks + grade;
    }

    if (totalMarks < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }

    return totalMarks / 5;
}

Also there should be no code after return statement as this code is unreachable and it will generate compilation error. Try to use some IDE (e.g. Netbeans, Intellij IDEA, Eclipse) so it will show you compilation errors as you type code.

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53839

You need to declare totalMarks outside of the for loop so that its scope is not limited to it.

Also, return must be the last instruction of your function:

public double CalculateResult() {
    int requiredGrade = 10; // some value
    int totalMarks = 0;
    for (int x = 0; x < 4; x++){
        int grade = input.nextInt();
        int totalMarks += grade;
    }
    double averageGrade = totalMarks / 5;
    if (averageGrade < requiredGrade){
        System.out.println("You didn't pass.");
    } 
    else{
        System.out.println("You passed.");
    }
    return averageGrade;
}

Upvotes: 3

Related Questions