I am looking for advice on making the following code shorter. Any help is appreciated.
public String determineWinner(String winner) {
winner = "";
if (playChoice.equals(compChoice))
winner = "The result is a tie.";
else if (compChoice.equals("R"))
if (playChoice.equals("S") || playChoice.equals("s"))
winner = "Computer wins because Rock beats Scissors.";
else
winner = "Player wins because Paper beats Rock";
else if (compChoice.equals("S"))
if (playChoice.equals("P") || playChoice.equals("p"))
winner = "Computer wins because Scissors beats Paper.";
else
winner = "Player wins because Rock beats Scissors";
else
if (playChoice.equals("R") || playChoice.equals("r"))
winner = "Computer wins because Paper beats Rock.";
else
winner = "Player wins because Scissors beats Paper.";
return winner;
}
Thanks
String.equalsIgnoreCase()in order to avoid checking twice for uppper and lower case. – Landei Dec 12 '12 at 13:59