2016-09-21 4 views
0

Ich versuche, ein Stein-Papier-Schere-Programm zu machen, das die besten zwei von dreien ist, wo der Computer zufällig eine 0-2 würfelt und jeder davon Stein, Papier oder Schere zugewiesen wird, und dann vergleicht er Der userInput und zählt einen Gewinn für Computer oder Spieler und addiert dann.Eingegebene Zeichenfolge auch einer Ganzzahl zugeordnet?

ABER, ich kann nicht herausfinden, wie man es macht, dass, wenn Benutzer "Schere" eingeben würde, das Programm wissen würde, dass es auch 2 zugewiesen wird (zu Vergleichszwecken).

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

Werfen Sie einen Blick auf Enums, sie werden Ihr Problem lösen –

Antwort

0

Also statt playerChoice = userInput.nextInt(); dies versuchen:

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; 
    } 
} 

Da Sie userInput.nextInt() und playerChoice ein int verwenden ist und Ints nur halten kann, müssen Sie Ihre Benutzereingabe analysieren. In diesem Fall wird versuchen, einen int in der Benutzereingabe zu finden. Wenn es nicht möglich ist, wird eine Ausnahme zurückgegeben. deshalb gibt es einen Try-Catch-Block. Wenn es kein int ist, wird nach jeder Zeichenfolge gesucht und der zugehörige int-Wert playerChoice oder -1 zugewiesen, wenn die Eingabe ungültig ist. Dann sollte der Rest Ihres Codes in der Lage sein, die playerChoice danach angemessen zu behandeln.

+0

Vielen Dank! Ich werde es schaffen! –

Verwandte Themen