0
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.

kurochenko
  • 1,214
  • 3
  • 19
  • 43
user2968861
  • 435
  • 1
  • 4
  • 6
  • a/ variables defined inside a loop can only be seen from inside the loop. That is part of what variable scope is. b/ you can't do anything after the return statement anyway. you prints are invalid. – njzk2 Feb 03 '15 at 22:25
  • as a side note, you may or may not want to take a look at http://stackoverflow.com/questions/4931892/why-does-the-division-of-two-integers-return-0-0-in-java – njzk2 Feb 03 '15 at 22:26

4 Answers4

3

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;
}
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
2

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.

kurochenko
  • 1,214
  • 3
  • 19
  • 43
1

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.

James Waddington
  • 2,894
  • 2
  • 15
  • 24
0

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.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Affe
  • 53
  • 1
  • 1
  • 7
  • Shouldn't it round UP? Also, your variable `d` is a `double`. It will actually hold places after the decimal. `INT` will not hold places after a decimal. – Twister1002 Feb 03 '15 at 22:38
  • No, integer devision floors. Even if it was 0.9 it would be rounded to 0. And yes, `d` is a `double`, but the statement right of the = is an integer, so it is first floored, then the `double d` gets assigned the result. `double d = i / (double) j` should do the trick. – Affe Feb 03 '15 at 22:58