2016-09-21 3 views
1

나는 바위 종이 가위 게임을 만들고 컴퓨터 또는 사용자가 게임을 다시하고 싶다면 프롬프트를 보여줘야한다. "예"이면 프로그램이 다시 시작되고 "아니오"일 경우 system.exit을 추측합니다.Java : 사용자에게 다시 재생할 것인지 묻습니다.

어떻게해야합니까?

public static void main(String[] args) { 
    Random r = new Random(); 
    int gameCount = 0; 
    int computerWins = 0; 
    int playerWins = 0; 
    int rock, paper, scissors; 
    rock = 1; 
    paper = 2; 
    scissors = 3; 
    int playerChoice = 0; 
    int computerChoice; 

    System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!"); 

    //While the game count is less than 3, the loop will repeat 
    while (gameCount < 3) { 
     computerChoice = r.nextInt(3) + 1; 
     System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\""); 
     String USER_Input = userInput.next(); 
     if (USER_Input.equalsIgnoreCase("Rock")) { 
      playerChoice = 1; 
     } 
     if (USER_Input.equalsIgnoreCase("Paper")) { 
      playerChoice = 2; 
     } 
     if (USER_Input.equalsIgnoreCase("Scissors")) { 
      playerChoice = 3; 
     } 
     //If player enters anything besides rock, paper, or scissors 
     if (playerChoice <= 0 || playerChoice > 3) { 
      System.out.println("That wasn't an option"); 
      computerWins++; 
      gameCount++; 
      System.out.println("Not a valid input! Computer Wins!\n" + 
        "Player has won " +playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 

      //The game goes on, and the winners are added up! 
     } else if (playerChoice == 1 && computerChoice == 2) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Rock v Paper! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 1) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Paper v Rock! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 3) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Paper v Scissors! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 2) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Scissors v Paper! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 1) { 
      computerWins++; 
      gameCount++; 
      System.out.println("Scissors v Rock! Computer Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 1 && computerChoice == 3) { 
      playerWins++; 
      gameCount++; 
      System.out.println("Rock v Scissors! Player Wins!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 1 && computerChoice == 1) { 
      gameCount++; 
      System.out.println("Rock v Rock! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 2 && computerChoice == 2) { 
      gameCount++; 
      System.out.println("Paper v Paper! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 3 && computerChoice == 3) { 
      gameCount++; 
      System.out.println("Paper v Paper! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } 

     //Check if game count reaches max games then chooses a winner 
     if (gameCount == 3 && computerWins > playerWins || computerWins == 2) { 
      System.out.println("The Computer Wins!"); 
      break; 
     } else if (gameCount == 3 && computerWins < playerWins || playerWins ==2) { 
      System.out.println("The Player Wins!"); 
     } else if (gameCount == 3 && computerWins == playerWins) { 
      System.out.println("The game is a tie!"); 
     } 
    } 
} 
+0

사용자 입력을 확인하고 "예"일 경우 메인() – ivan

+0

''그건 옵션이 아니 었습니다. "=> computerWins ++'+1을 얻습니다. –

+0

하하 감사합니다. fr_andres;) 컴퓨터가 항상 이긴다! @ivan 메인 메서드를 다시 호출 할 수 있는지 몰랐습니다. 흥미 롭습니다. –

답변

3

전체 로직이 main() - 메소드 내에 있습니다. while() 부분을 play()와 같은 새로운 메서드로 옮기는 것이 좋습니다.

public static void main(String args[]) { 
     Scanner inputScanner = new Scanner(System.in); 
     String userInput = ""; 
     do { 
      play(); 
      System.out.print("Do you want to play again ([Y]/n)? "); 
      userInput = inputScanner.nextLine(); 
      System.out.println(userInput); 
     } while (userInput.equals("Y") || userInput.equals("")); 
} 

HTH, SiS는

+0

미안 해요. 메인 메서드 밖에 다른 메서드를 아직 만들지 못했습니다. 이 코드를 다른 코드 앞에두면 사용자가 다시 재생할 것을 요구하지 않습니다. 나는 정확하게 이것을 정확하게 구현하기 위해 어떻게 코드를 구현하는지 알아 내려고 노력하고있다. –

+0

안녕 Christopher. main() 메소드가 프로그램 진입 점이 아니기 때문에 그렇지 않습니다. 메인의 1 번과 2 번 라인은 "그냥 변수 선언"입니다. 3 번 줄은 요청한 "다시 재생"- 루프의 시작입니다. 그런 다음 4 행에서 "play()"새 함수 private static void play() {전체 루프 양식에서 처음 게시물}이 실행됩니다. 이 시점에서 아무런 메시지가 발생하지 않았습니다. 첫 번째 프롬프트는 play()의 첫 번째 호출 이후 인 5 번째 줄과 6 번째 줄과 함께 제공됩니다. – StehtimSchilf

0

가능한 솔루션에 게임을 배치하는 것입니다 : 주에서

() 메시지를 자극하고 키보드에서 입력을 읽는) (새로운 동안을 구현 다른 while 루프 (gameCount의 엔벨로프를 감싸는)와 while 루프 내에서 변수에 대한 switch 문을 작성하면 gameContinue이됩니다. 게임이 끝나면 사용자는 계속할지 여부를 묻는 메시지를 받게됩니다. 그들의 대답을 바탕으로 변수 gameContinue의 값을 변경하면됩니다.

관련 문제