Java PlayerDecision故障

Java PlayerDecision故障,java,Java,我正试着写一个石头剪刀布游戏,我想我差不多完成了。但是,调用最后一个方法时遇到问题。 这是我目前的代码: import java.util.*; public class RPS { public static void main(String[] args) { PlayerChoice(); ComputerChoice(); System.out.println("You played " + playerDecision + ". The co

我正试着写一个石头剪刀布游戏,我想我差不多完成了。但是,调用最后一个方法时遇到问题。 这是我目前的代码:

import java.util.*;

public class RPS {
   public static void main(String[] args) {
      PlayerChoice();
      ComputerChoice();
      System.out.println("You played " + playerDecision + ". The computer played " + computerPlay +".");
   }
   public static void PlayerChoice() {
      System.out.print("Type R(ock), P(aper) or S(cissors): ");
      int r = "rock";
      int p = "paper";
      int s = "scissors";
      String r = console.r();
      string p = console.p();
      String s = console.s();
      Random rand = new Random();
      int playerDecision = nextInt();
         if(playerDecision.equalsIgnoreCase("r")){
            System.out.println("rock");
         }else if(playerDecision.equalsIgnoreCase("p")){
            System.out.println("paper");
         }else if(playerDecision.equalsIgnoreCase("s")){
            System.out.println("scissors");
         }
         while(!nextInt = 'r' || 'p' || 's'){
            System.out.println("Invalid answer. Re-type R, P or S: ");
         }
         System.out.print(playerDecision);
   }  
   public static void ComputerChoice() {
      int rock = 1, paper = 2, scissors = 3;
      Random rand = new Random();
      int computerPlay = rand.nextInt(3) + 1;

      if(computerPlay == 0){
         System.out.println("rock");
      }else if(computerPlay == 1){
         System.out.println("paper");
      }else if(computerPlay == 2){
         System.out.println("scissors");
      }
      System.out.print(computerPlay);
   }
}
但是,我的代码找不到playerDecision或computerPlay。我希望玩家选择的方法能让玩家做出决定,就像名字里说的那样。如果他们输入R,则无论大小写,都要输入石头,P代表纸,S代表剪刀。计算机选择法似乎运作良好。 有人知道我在做决定时做错了什么吗?我不知道为什么它读不好,我也没有主意了。
有人给我提建议吗?

你的快捷语法是非法的。你的逻辑有点不正确。虽然nextInt='r'| |'p'| |'s'{应该是

while (nextInt != 'r' && nextInt != 'p' && nextInt != 's') {
或者应用德摩根定律,得到

while (!(nextInt == 'r' || nextInt == 'p' || nextInt == 's')) {

此外,ComputerChoice也不能正常工作。您正在生成1到3(包括1到3)范围内的值,但您只检查0、1和2。删除int computerPlay=rand.nextInt3+1;中的+1,或者修复if逻辑。

好的。我以为我已经编辑了随机值,但我一定错过了。我将检查这些&/|值。谢谢ose似乎都解决了,但我的主要方法调用仍然为playerChoice和computerChoice调用显示了找不到符号错误。你对此有什么想法吗?找不到符号通常是一个拼写错误的变量或方法名。这些拼写正确。我想这与其他方法中的找不到符号有关,但是我想通过将这些方法调用到main中可以解决这个问题。我想我需要找到一种方法将它们放入第一个语句中,不是吗?@markspace检查你的大小写。你不能将它命名为PlayerChoice,然后像PlayerChoice一样调用它。1.你拼写错了字符串2.playerDecision、computerPlay或nextInt从未定义为computerterPlay和playerDecision仅在它们定义的方法中可用,您可能希望返回值或使用实例变量。您可能希望使用nextInt的扫描程序,但我不知道您在做什么3。正如回答中所指出的,您的逻辑是错误的。感谢您指出字符串拼写错误…这uld交上来很尴尬。我甚至没有注意到这一点。@Zachary你是对的。我定义了一些,但nextInt是在playerDecision中定义的,另外两个没有完全定义。