2015-04-24 3 views
-1

поэтому, когда я запускаю свой код, он продолжает говорить, что это связь, потому что я думаю, что есть проблема в одном из моих методов.Неполадка получения результатов от бумаги для бумажных ножниц код (java)

Вот весь мой код:

String userChoice = ""; 
     String computerChoice = ""; 
     System.out.println("Welcome to Rock, Paper, Scissors."); 
     System.out.println("The rules of the game are Rock breaks Scissors, " 
       + "Scissors cut Paper, and Paper covers Rock. In this game, " 
       + "the user will play against the computer."); 
     System.out.println("The legend for this game is: R = rock, P = paper," 
       + " and S = scissors."); 

     generateUserChoice(); 
     generateComputerChoice(); 
     determiningOutcome(userChoice, computerChoice); 
     repeatGame(userChoice, computerChoice); 

    } 

    public static String generateComputerChoice() { 
     String computerChoice = ""; 
     int computerIntChoice; 

     Random generator = new Random(); 
     computerIntChoice = generator.nextInt(3) + 1; 

     if (computerIntChoice == 1) { 
      computerChoice = "R"; 
     } else if (computerIntChoice == 2) { 
      computerChoice = "P"; 
     } else if (computerIntChoice == 3) { 
      computerChoice = "S"; 
     } 
     System.out.println("The computer played " + computerChoice); 
     return computerChoice; 

    } 

    public static String generateUserChoice() { 
     String userChoice; 
     Scanner input = new Scanner(System.in); 
     System.out.println("Please Enter your choice:"); 
     userChoice = input.nextLine(); 
     userChoice = userChoice.toUpperCase(); 

     return userChoice; 

    } 

    public static void determiningOutcome(String userChoice, String computerChoice) { 

     if (userChoice.equals(computerChoice)) { 
      System.out.println("It is a tie!"); 
     } else if (userChoice.equalsIgnoreCase("R") && computerChoice.equalsIgnoreCase("S")) { 
      System.out.println("Rock beats Scissors, you win!!"); 
     } else if (userChoice.equalsIgnoreCase("P") && computerChoice.equalsIgnoreCase("R")) { 
      System.out.println("Paper beats Rock, you win!!"); 
     } else if (userChoice.equalsIgnoreCase("S") && computerChoice.equalsIgnoreCase("P")) { 
      System.out.println("Scissors beats Paper, you win!!"); 
     } else if (computerChoice.equalsIgnoreCase("R") && userChoice.equalsIgnoreCase("S")) { 
      System.out.println("Rock beats Scissors, you lose."); 
     } else if (computerChoice.equalsIgnoreCase("P") && userChoice.equalsIgnoreCase("R")) { 
      System.out.println("Paper beats Rock, you lose."); 
     } else if (computerChoice.equalsIgnoreCase("S") && userChoice.equalsIgnoreCase("P")) { 
      System.out.println("Scissors beats Paper, you lose."); 
     } else { 
      System.out.println("Sorry, invalid choice."); 
     } 
    } 

    public static int repeatGame(String userChoice, String computerChoice) { 
     int playAgain; 

     Scanner input = new Scanner(System.in); 
      System.out.println("Would you like to play again? 1 = yes and 2 = no"); 
     playAgain = input.nextInt(); 

     if (playAgain == 1) { 
     System.out.println("Would you like to play again? 1 = yes and 2 = no"); 
      generateUserChoice(); 
      generateComputerChoice(); 
      determiningOutcome(userChoice, computerChoice); 

     }else { 
      System.out.println("Thank you for playing!!"); 
     } 

    return playAgain;  
    } 

    } 

однако я думаю, что проблема заключается в generateComputerChoice части моего кода

Заранее спасибо

+0

Вы печатаете выбор компьютера. Что он печатает? – stark

+1

Вы не назначаете переменные выбора ('userChoice' и' computerChoice'); они остаются пустыми. –

ответ

1

Проблема заключается в том, что userChoice и computerChoice является всегда был пуст, поэтому он всегда был галстуком.

Вам необходимо назначить userChoice и computerChoice из возвращаемого значения методов.

так:

userChoice = generateUserChoice(); 
computerChoice = generateComputerChoice();