0

The score is increasing until 40 using the increaseScore() method.

If a boolean flag is true, the score will be reduced by 10 (so, in this case, it will be 30), and display score = 30 and grade = D in toString().

But I only can get score = 40 and grade = D in toString().

Is it possible to add return score; after score = score - 10; in findGrade()?

public int increaseScore(){
   score = score + 10;
   return score;
    }

public char findGrade(){
    if (flag)
        score = score - 10;

    char grade = 'A';

    if (80<=score)
        grade = 'A';
    else if (60<=score && score <= 79)
        grade = 'B';
    else if (40<=score && score <= 69)
        grade = 'C';
    else 
        grade ='D';

    return grade;
}

public String toString(){
    return teamName + " " + score + " " + findGrade();
}
5
  • 4
    Without getting into detail about what you are trying to do, simply put, a java method can only return a single item. (Note that you can return an array or something that in itself contains multiple items) Commented Apr 29, 2014 at 3:42
  • I'm not 100% clear on what you're trying to accomplish. score is declared as a field (well, I hope it is, since it's not declared anywhere else), so you don't really need to worry about returning multiple values from findGrade(). You already have access to the field, and you're using it in toString(). If you could provide more clarification as to why you think you need two return values, perhaps one of us could answer your question better? Perhaps it isn't a raw duplicate of returning two things from a function? Commented Apr 29, 2014 at 3:52
  • if you want to return more than one value, Put them in an Array and return the array Commented Apr 29, 2014 at 4:26
  • 1
    I've started up a meta discussion on this post; you can find it here. I believe that it's actually an XY problem; there's a better way to solve this issue as opposed to returning two values. Commented Apr 29, 2014 at 5:09
  • You need to make sure that findGrade gets called before toString uses score. Perhaps public String toString(){ char grade = findGrade(); return teamName + " " + score + " " + grade; } Commented Dec 31, 2014 at 1:58

1 Answer 1

1

you can add another method for toString() use:

public String findGradeString() {
    return score + ":" + findGrade();
}
public String toString(){
    return teamName + " " + score + " " + findGradeString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This continues to read score before the call to findGrade(), so it doesn't solve the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.