一种更有效的方式,占用更少的空间?这是一个方法顺便说一句

时间:2013-12-16 09:06:18

标签: java

这是Rock Paper Scissors项目代码的一部分,我想知道是否有办法做到这一点,占用更少的空间。优选地也更有效。基本上这是一种方法,这种方法的作用是比较用户输入以查看一方是否胜过另一方。     感谢。

public String determineWinner()
{
    String winner = "yolo"; //if fail

    if(compChoice.equals("S") && (playChoice.equals("s")))
    {
        winner = "nobody.  There was a tie because you guessed the same thing.";
    }

    if(compChoice.equals("P") && (playChoice.equals("p")))
    {
        winner = "nobody.  There was a tie because you guessed the same thing.";
    }

    if(compChoice.equals("R") && (playChoice.equals("r")))
    {
        winner = "nobody.  There was a tie because you guessed the same thing.";
    }

    if(compChoice.equals(playChoice)) //R R, R P, R S
    {
        winner = "nobody.  There was a tie because you guessed the same thing.";
    }

    if(compChoice.equals("R") && (playChoice.equals("P") || playChoice.equals("p"))) //R P
    {
        winner = "player because Paper beats Rock.";
    }

    if(compChoice.equals("R") && (playChoice.equals("S") || playChoice.equals("s"))) //R S
    {
        winner = "computer because Rock beats Scissors.";
    }

    if(compChoice.equals("P") && (playChoice.equals("R") || playChoice.equals("r")))//P R
    {
        winner = "computer because Paper beats Rock.";
    }        

    if(compChoice.equals("P") && (playChoice.equals("S") || playChoice.equals("s")))//P S
    {
        winner = "player because Scissors beats Paper.";
    }        

    if(compChoice.equals("S") && (playChoice.equals("R") || playChoice.equals("r"))) //S R 
    {
        winner = "player because Rock beats Scissors.";
    }       

    if(compChoice.equals("S") && (playChoice.equals("P") || playChoice.equals("p"))) //S P
    {
        winner = "computer because Scissors beats Paper.";
    }      
    return winner;
}

9 个答案:

答案 0 :(得分:5)

你可以这样做

String[] words = "Rock,Paper,Scissors".split(",");
// turn the choice into an index where higher wins. 
// i.e. 0 < 1 < 2 < 0 (using clock arithmetic)
int human = "RPS".indexOf(playChoice.toUpperCase());
int comp = "RPS".indexOf(compChoice.toUpperCase());
// if the index is the same, no winner
if (human == comp)
    return "No winner, choices are the same";
// if the human has the higher index (using clock arithmetic), the human wins.
if (human == (comp+1) % 3)
    return "Human winner as " + words[human] + " beats " + words[comp];
// otherwise the computer must have won.
return "Computer winner as " + words[comp] + " beats " + words[human];

答案 1 :(得分:2)

String winner = "";

String playersChoice = playChoice.toUpperCase();

if(compChoice.equals(playersChoice))
    return "tie";

switch(compChoice) {

case "S":
    switch(playersChoice) {
    case "P":
        winner = "computer";
        break;
    case "R":
        winner = "player";
        break;
    }
    break;

case "P":
    switch(playersChoice) {
    case "S":
        winner = "player";
        break;
    case "R":
        winner = "computer";
        break;
    }
    break;

case "R":
    switch(playersChoice) {
    case "S":
        winner = "computer";
        break;
    case "P":
        winner = "player";
        break;
    }
    break;
}

return winner;

答案 2 :(得分:1)

为了提高效率,我建议您使用嵌套的if else语句,而不是仅使用

之类的语句
if {} else{if(){} else{....}}

在您的代码中,将执行每个if循环,这会降低效率,因此如果使用嵌套,则使用嵌套 Useful link for nested if else

答案 3 :(得分:0)

您可以使用更高效的char比较

char cc = compChoice.charAt(0);
char pc = playChoice.charAt(0);

if (cc == 'S' && pc == 's') {
   ...
} else if (
   ...

答案 4 :(得分:0)

JqueryLearner是对的,但如果你这样做,你会得到更清洁的if-else

if () {
} else if () {
} else if () {
} else {
}

您需要更多地考虑一般规则和简化,而不是考虑具体细节。

例如,如果您接受输入并将其设置为小写,则只需要检查r,s等,而不是R,S。

如果你然后检查两者是否相等(你不关心它的r和r,或s和s - 你只需要它们是相同的)来处理所有的绘制。

然后你只需要检查r&gt; s&gt; p>河

答案 5 :(得分:0)

您可以使用if子句检查计算机和播放器的选择是否相同,例如:

if(compChoice.equalsIgnoreCase(playerChoice)){
 return "TIE"
}

注意我使用ignoreCase来避免你的多个if用于较低和较高的情况。

为选项创建一个数组:[纸,岩石,剪刀] 现在纸张摇滚,摇滚剪刀和剪刀击败纸张。 你必须从这个数组计算comp和player选择的索引,然后使用以下公式得到结果:

int result = (playerChoiceNum - compChoiceNum) % 3

playerChoiceNum和compChoiceNum是数组的索引。 如果结果为0则为平局,否则如果结果为负,则获胜,否则结果为正玩家获胜。

答案 6 :(得分:0)

在这里使用enum整个游戏怎么样......

public static enum RPS {
  ROCK, PAPER, SCISSORS;

  // Simple method to get the appropriate enum.
  public static RPS fromString(String in) {
    if (in.toLowerCase().startsWith("r")) {
      return ROCK;
    } else if (in.toLowerCase().startsWith("p")) {
      return PAPER;
    } else if (in.toLowerCase().startsWith("s")) {
      return SCISSORS;
    }
    return null;
  }

  // Calculate the winner in a contest.
  public Boolean wins(RPS in) {
    if (this == in) { // Tie!
      return null;
    }
    switch (this) {
    case ROCK:
      if (in == SCISSORS) { // Rock beats scissors.
        return true;
      }
      return false;
    case PAPER:
      if (in == ROCK) { // Paper beats rock.
        return true;
      }
      return false;
    case SCISSORS: 
      if (in == PAPER) { // Scissors beats paper.
        return true;
      }
      return false;
    }
    return null;
  }
}

public static void main(String[] args) {
  Random r = new Random(System.currentTimeMillis());
  String[] choices = new String[] { "R", "P", "S" };
  Scanner in = new Scanner(System.in);
  while (in.hasNextLine()) {
    System.out.println("Please enter (R)ock, (P)aper "
            + "or (S)cissors to play. (Q)uit.");
    String c = in.nextLine();
    c = c.trim();
    if (c.toLowerCase().startsWith("q")) {
      break;
    }
    RPS player = RPS.fromString(c);
    RPS computer = RPS.fromString(choices[r
        .nextInt(choices.length)]);
    System.out.println("Computer picked " + computer);

    if (player.wins(computer) == null) {
      System.out.println("It's a Tie");
    } else if (player.wins(computer)) {
      System.out.println("You won");
    } else {
      System.out.println("The comptuer won");
    }
  }
}

答案 7 :(得分:0)

好的,首先你的问题是误导性的   但 如果您使用.equalsIgnoreCase()这将允许您检查两者,
纸张的“P”和“p”因此停止,您必须同时检查“P”和“p” if else if else 在这里可以更好地工作:
if一个语句不正确它将转到else if来检查这是否为真,如果不是这样,它将转到下一个语句,看看哪个是真的。 然后,else表示如果上述ifelse ifs均不为真,则必须选择此选项。

    if(compChoice.equalsIgnoreCase(playChoice)) //R R, R P, R S
    {
        winner = "nobody.  There was a tie because you guessed the same thing.";
    }

    else if(compChoice.equals("R") && (playChoice.equalsIgnoreCase("P"))) //R P
    {
        winner = "player because Paper beats Rock.";
    }

    else if(compChoice.equals("R") && (playChoice.equalsIgnoreCase("S"))) //R S
    {
        winner = "computer because Rock beats Scissors.";
    }

    else if(compChoice.equals("P") && (playChoice.equalsIgnoreCase("R")))//P R
    {
        winner = "computer because Paper beats Rock.";
    }        

    else if(compChoice.equals("P") && (playChoice.equalsIgnoreCase("S")))//P S
    {
        winner = "player because Scissors beats Paper.";
    }        

    else if(compChoice.equals("S") && (playChoice.equalsIgnoreCase("R"))) //S R 
    {
        winner = "player because Rock beats Scissors.";
    }       

    else//S P
    {
        winner = "computer because Scissors beats Paper.";
    }      
    return winner;

这是短暂而有效的,我可以做到。

答案 8 :(得分:0)

这也适用

   public String determineWinner() {
       Map<String, String> shortcutMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
       Map<String, Map<String, Integer>> winMap = new TreeMap<String, Map<String, Integer>>(String.CASE_INSENSITIVE_ORDER);

       shortcutMap.put("R", "Rock");
       shortcutMap.put("P", "Paper");
       shortcutMap.put("S", "Scissors");

       winMap.put("R", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
       winMap.put("P", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));
       winMap.put("S", new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER));

       winMap.get("R").put("R",  0);  // Rock draw against Rock,
       winMap.get("R").put("P", -1);  // Rock loose against Paper,
       winMap.get("R").put("S",  1);  // Rock win against Scissors,

       winMap.get("P").put("R",  1);  // Paper win against Rock,
       winMap.get("P").put("P",  0);  // Paper draw against Paper,
       winMap.get("P").put("S", -1);  // Paper loose against Scissors,

       winMap.get("S").put("R", -1);  // Scissors loose against Rock,
       winMap.get("S").put("P",  1);  // Scissors win against Paper,
       winMap.get("S").put("S",  0);  // Scissors draw against Scissors,

       String winner = "yolo"; // if fail

       Integer result = winMap.get(compChoice).get(playChoice);
       if (result > 0) {
           winner = "computer because " + shortcutMap.get(compChoice) + " beats " + shortcutMap.get(playChoice) + ".";
       }
       else if (result < 0) {
           winner = "player because " + shortcutMap.get(playChoice) + " beats " + shortcutMap.get(compChoice) + ".";
       }
       else {
           winner = "nobody.  There was a tie because you guessed the same thing.";
       }

       return winner;
   }