石头-剪刀布无效的输入循环不起作用


似乎

无法修复我的代码,每当我尝试输入值时,它都会显示为错误。另外,我使用 ibio 的东西来获取输入,所以在进行代码更正时请忽略它,顺便说一下,谢谢。这将对我有很大帮助,因为我似乎不知道该怎么做......

编辑:我遇到了另一个问题,用户输入总是变成剪刀,无论他们输入什么。 例如:

Options: Rock, Paper, Scissors
Your choice? Rock
You have selected s

 public class rockPaperScissors
 {
    public static void main (String args[])
    {
        new rockPaperScissors ();
    }
     public rockPaperScissors ()
    {
        int cPoint = 0;
        int uPoint = 0;
        int game = 1;
        char playAgain = 'y';
        System.out.println ("-----Rock, Paper, Scissors------n");
        while (playAgain == 'y')
    {
            char user = userChoice ();
            System.out.println ("You have selected " + user);
            char comp = compChoice ();
            System.out.println ("The computer has selected " + comp);
            char win = winner (comp, user);
            if (win == 'c')
            {
                cPoint++;
                System.out.println ("nThe computer wins!");
            }
            else if (win == 'u')
            {
                uPoint++;
                System.out.println ("nYou win!");
            }
            else
                System.out.println ("nThere is a tie!");
            System.out.println ("Points: You: " + uPoint + " Computer: " +     cPoint);
            playAgain = IBIO.inputChar ("nPlay again? (y/n) ");
            System.out.println ("");
        }
        System.out.println ("Goodbye!");
    }

    public boolean isValid (String c)
    {
        /* All valid data:
               Rock, ROCK, rock, r, R
               Paper, PAPER, paper, p, P
               Scissors, SCISSORS, scissors, s, S
           return true if valid, false otherwise
        */
        if (c=="Rock" || c=="ROCK" || c == "rock" || c=="r" || c=="R" ||     c=="Scissors" || c=="SCISSORS" || c=="scissors" || c=="s" || c=="S" || c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
        return true;
        else
        return false;
    }

    public char userChoice ()
    { //returns r, p or s, based on the user's choice
        //print options: rock, paper, scissors
        System.out.println ("Options: Rock, Paper, Scissors");
        //ask for user's choice. will need to store in String
        String c = IBIO.inputString ("Your choice? ");
        //Loop: if invalid input, ask again
        //stopping condition is the isValid method!!
        //something like: while(!isValid(choice))
        while (!isValid(c))
         {
          System.out.println ("That choice is invalid. Try another choice.");
          c = IBIO.inputString ("Your choice? ");
         }
        //If: to standardize values
        //if you've got one of Rock, ROCK, rock, r, R, then return 'r'.
        if (c=="Rock" || c=="ROCK" || c=="rock" || c=="r" || c=="R")
           return 'r';
        //else if you've got one of Paper, PAPER, paper, p, P, then return 'p'.
        else if (c=="Paper" || c=="PAPER" || c=="paper" || c=="p" || c=="P")
           return 'p';
        //else return 's';
        else
           return 's';
    }

    public char compChoice ()
    { //make a random number between 1 and 3
         //if the number is 1, return r; 2 return s; 3 return p
    int num = (int)(Math.random () * 3) + 1;
    if (num == 1)
        return 'r';
    else if (num == 2)
        return 's';
    else
        return 'p';
}

    public char winner (char comp, char user)
    { //comp and user both hold one of r, s, p
        //returns c for computer, u for user and b for both
    if ((comp == 'r' && user == 's') || (comp == 's' && user == 'r') || (comp == 'p' && user == 's'))
        return 'c';
    if ((user == 'r' && comp == 's') || (user == 's' && comp == 'r') || (user == 'p' && comp == 's')) 
        return 'u';
    else
        return 'b';
    }
}

你的问题很明显,每次你使用 == 比较字符串时,它都会返回 false。我告诉你查看副本来解决这个问题,但我强烈建议你用一些方法把你的代码分成更小的部分。

  public boolean isRock(String input) {
      String lower = input.toLowerCase();
      return lower.equals("r") || lower.equals("rock");
  }
  public boolean isPaper(String input) {
      String lower = input.toLowerCase();
      return lower.equals("p") || lower.equals("paper");
  }
  public boolean isScissors(String input) {
      String lower = input.toLowerCase();
      return lower.equals("s") || lower.equals("scissors");
  }
  public boolean isValid(String input) {
      return isRock(input) || isPaper(input) || isScissors(input);
  } 

这会将您的其他条件简化为

if (isRock(c))
   return 'r';
else if (isPaper(c))
   return 'p';
else
   return 's';

最新更新