2016-09-21 197 views
0

我試圖製作一個最好的三分之二的電腦剪刀程序,其中計算機隨機滾動0-2,每個分配給搖滾,紙張或剪刀,然後比較userInput並計算電腦或播放器的勝利,然後將其累加起來。輸入的字符串也分配給一個整數?

但是,我不知道如何使它,如果用戶輸入「剪刀」程序會知道它也被分配到2(爲了比較目的)。

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

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

    while (gameCount >= 0 && gameCount < 3) 
    { 
     System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\""); 
     break; 
    } 
     playerChoice = userInput.nextInt() 

     //If player enters anything besides rock, paper, or scissors 
     if (playerChoice < 0 || playerChoice >= 3) { 
      System.out.println("That wasn't an option"); 
      computerWins++; 
      gameCount++; 

      //The game goes on, and the winners are added up! 
     } else if (playerChoice == 0 && computerChoice == 1) { 
      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 == 1 && computerChoice == 0) { 
      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 == 1 && computerChoice == 2) { 
      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 == 2 && computerChoice == 1) { 
      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 == 2 && computerChoice == 0) { 
      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 == 0 && computerChoice == 2) { 
      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 == 0 && computerChoice == 0) { 
      gameCount++; 
      System.out.println("Rock v Rock! Tie!\n" + 
        "Player has won " + playerWins + " times and the computer " + 
        "has won " + computerWins + " times"); 
     } else if (playerChoice == 1 && computerChoice == 1) { 
      gameCount++; 
      System.out.println("Paper v Paper! 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"); 
     } 

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

看看枚舉,他們會解決你的問題 –

回答

0

所以不是playerChoice = userInput.nextInt();試試這個:

Scanner sc = new Scanner(System.in); 
String input = sc.nextLine(); 
try { 
    playerChoice = Integer.parseInt(input); 
} catch (NumberFormatException e) { 
    if (input.equalsIgnoreCase("rock")) { 
     playerChoice = rock; 
    } else if (input.equalsIgnoreCase("paper")) { 
     playerChoice = paper; 
    } else if (input.equalsIgnoreCase("scissors")) { 
     playerChoice = scissors; 
    } else { 
     // if input is invalid 
     playerChoice = -1; 
    } 
} 

由於您使用userInput.nextInt()playerChoice是一個int,只能容納整數,你需要分析你的用戶的輸入。在這種情況下,Integer.parseInt(input)將嘗試在用戶輸入中查找int。如果不能,它將返回一個異常;這就是爲什麼有一個try-catch塊。如果它不是int,則它將查找每個字符串並將關聯的int值分配給playerChoice或-1,如果輸入無效。然後,其他代碼應該能夠在此之後適當地處理playerChoice。

+0

謝謝你!我會做到這一點! –

相關問題