-4

why do i get an error, is it because im missing a return after the if checks? could i resolve it by setting one last return to null

public static String whoWins(rockPaperScissors user, rockPaperScissors computer){

    if(user == ROCK && computer == SCISSORSS){
        return "user wins";
    }
    if(user == PAPER && computer == ROCK){
        return " user wins";
    }
    if(user == SCISSORSS && computer == PAPER){
        return "user wins";
    }
    if(computer == ROCK && user == SCISSORSS){
        return "computer wins";
    }
    if(computer == PAPER && user == ROCK){
        return "computer wins";
    }
    if (computer == SCISSORSS && user == PAPER){
        return "computer wins";
    }
}
  • 1
    what if `user == null`? Then it wouldn't know what to return. – Rogue Mar 29 '17 at 16:00
  • yes you need a non-conditional return – Alex Mar 29 '17 at 16:00
  • 2
    `"why do i get an error"` - Actually bothering to read the error message might help answer that. The system is telling you what the problem is. Why would you be ignoring it and asking us instead? – David Mar 29 '17 at 16:00
  • 1
    If you already know the answer, then why the question? – z m Mar 29 '17 at 16:00
  • 2
    `why do i get an error, is it because im missing a return after the if checks? could i resolve it by setting one last return to null` Why didn't you just **try exactly that and find ouit**? – tnw Mar 29 '17 at 16:02
  • i guess my wording was poor, its not an error. Its telling me that im missing a return statement. And that return statement needs to be outside of the conditional checks. – user1066367 Mar 29 '17 at 16:05
  • How is that not an error? – tnw Mar 29 '17 at 16:09
  • i just noticed im not handling ties, i can add default conditional check and add a return "tie" statement – user1066367 Mar 29 '17 at 16:16
  • that doesnt work, as it continues to fall trough the if logic and reads only the tie statement. – user1066367 Mar 29 '17 at 16:18
  • Nice got it to work!. Thanks alot for your help guys. – user1066367 Mar 29 '17 at 16:24

1 Answers1

2

You get an error because your method does not always return a result. For example, what happens if user == null?

In my opinion, the cleanest way to handle this is to throw an exception because you should not reach the last line. IllegalArgumentException is a good candidate because the problem would come from invalid arguments passed to the method.

public static String whoWins(rockPaperScissors user, rockPaperScissors computer){
  [...]

  if (computer == SCISSORSS && user == PAPER){
    return "computer wins";
  }

  throw new IllegalArgumentException(...);
}
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • im doing this as a gui, and i should account for every possible combination of the rock paper sicssorss game. – user1066367 Mar 29 '17 at 16:20
  • @user1066367 `null` is not a valid combination but is syntactically valid. That is why you syntactically need to handle the case even if you *know* (but the compiler does not) that it cannot happen. – Arnaud Denoyelle Mar 29 '17 at 16:28