+ 3

Can someone explain what the "return -1" and what the "return finalScore" does to this code? Thanks!

public static void main(String [] args) { boolean game over = true; int score = 80; int levelCompleted = 5; int bonus = 3; calculateScore(gameOver, score, levelCompleted, bonus); score = 10; levelCompleted = 8; bonus = 2; calculateScore(gameOver, score, levelCompleted, bonus); } public static int calculateScore(boolean gameOver, int score, in levelCompleted, int bonus) { if (gameOver) { int finalScore = score + (levelCompleted * bonus); System.out.println(finalScore); return finalScore ; } return -1; } }

20th Mar 2017, 8:08 PM
PaperGami T.
PaperGami T. - avatar
1 Respuesta
+ 4
note: error on line 4, extra space between level and completed: "level completed" return is what you will receive when calling the function. So when you say: calculateScore(....); The value of calculate score (since the function has an int return type) is -1 if the game is not over. If the game is over, it is the final score. Try: System.out.println(calculateScore(..)); to see what I mean. (fill the parameters accordingly). Note* Since this function is supposed to calculate the score it would make more sense to return the current score instead of returning -1. Or rename it to finalScore() or something like that. (so -1 means game isnt over). I suggest you review return types of functions.
20th Mar 2017, 8:50 PM
Rrestoring faith
Rrestoring faith - avatar