Cumulative project 3 return statements in the same method body

I am so very lost I truly believe that this code should be working but i am getting errors that i can’t seem to fix.
public class MainActivity {

// Add generateRandomNumber() here
public int generateRandomNumber(int max){
double value = Math.random() * max;
double number = (double) value;
int result = (int) number;
return result;

// Add getGameOverMessage() here
public String getGameOverMessage(int totalCorrect, int totalQuestions) {
if (totalCorrect == totalQuestions) {
return “You got all " + totalQuestions + " right! You won!”;
} else {
return "You got " + totalCorrect + " right out of " + totalQuestions + “. Better luck next time!”;
}
}

}

Have I missed something obvious? I am trying to get this getGameOver message statement to display properly, but this is the error i am getting:

MainActivity.java:11: error: illegal start of expression
public String getGameOverMessage(int totalCorrect, int totalQuestions) {
^
1 error

Look at your generateRandomNumber function. Do you ever close it?

yes closes at the very bottom

i am having difficulty with the getGameOverMessage

Look at the following code:

public int generateRandomNumber(int max){
  double value = Math.random() * max;
   double number = (double) value;
   int result = (int) number;
   return result;

The function has not been closed. As a general rule, one cannot declare a function inside another function.

For example:

public int someFunc() {
  public string anotherFunc(){
    //code
    return "Hello!";
  }
  return 5;
}

Cannot be done, but:

public int someFunc(){
//code
return 5;
}

public string anotherFunc(){
  //code
  return "Hello!";
}

Can be done.

i closed it in the wrong place, tyvm

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.