2016-09-21 50 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

谢谢你!我会做到这一点! –

相关问题