2014-02-14 76 views
0

我已經寫在BlueJ岩石剪刀代碼和迄今爲止一切工作很好......除了一件事。當我和電腦綁在一起時,遊戲不會告訴我。它會告訴我電腦使用了什麼,它會告訴我,如果我贏了或輸了。但是,如果我綁定了,它只會告訴我電腦使用了什麼,而忽略了這條領帶。代碼如下。 ~~~我的岩石紙剪刀遊戲將不會顯示關係

/** 
*A simple game of Rock Paper Scissors. 
* 
*@author Erik Ingvoldsen 
*@version 1.0 
*/ 

import java.util.Scanner; 
import java.util.Random; 
public class Rock 
{ 
public static void main(String[] args) 
{ 
String playAgain = "4";  
do 
    { 
    String personPlay; 
    String computerPlay = ""; 
    int computerInt; 
    String response; 
    Scanner scan = new Scanner(System.in); 
    Random generator = new Random(); 
    System.out.println(" "); 
    System.out.println("Press 1 for Rock, 2 for Scissors, 3 for Paper, and 4 to quit."); 
    System.out.println(); 
    computerInt = generator.nextInt(3)+1; 
    if (computerInt == 1) 
     computerPlay = "Rock"; 
    else if (computerInt == 2) 
     computerPlay = "Scissors"; 
    else if (computerInt == 3) 
     computerPlay = "Paper"; 
    //1=Rock 2=Scissors 3=Paper 
    {System.out.println("Rock Paper Scissors: "); 
    {personPlay = scan.next(); 
    System.out.println("Computer chooses: " + computerPlay); 
    if (personPlay.equals(computerPlay)){ 
     System.out.println("It's a tie!");} 
    else if (personPlay.equals("1")){ 
     if (computerPlay.equals("Paper")) 
      System.out.println("Paper beats rock. Loser."); 
    else if (computerPlay.equals("Scissors")) 
      System.out.println("Rock beats scissors. Winner.");} 
    else if(personPlay.equals("2")){ 
     if (computerPlay.equals("Paper")) 
      System.out.println("Scissors beats paper. Winner"); 
    else if (computerPlay.equals("Rock")) 
      System.out.println("Rock beats scissors. Loser.");} 
    else if (personPlay.equals("3")) { 
     if (computerPlay.equals("Rock")) 
      System.out.println("Paper beats rock. Winner."); 
    else if (computerPlay.equals("Scissors")) 
      System.out.println("Scissors beats paper. Loser.");} 
    else 
System.out.println("Not a valid key. Cheaters never win.");} 
System.out.println("Press 4 to quit or any other key to continue."); 
Scanner End = new Scanner(System.in); 
String quit=End.nextLine(); 
if(quit.equals("4")) 
{ 
System.exit(0);} 
} 
}while(true); 
} 
} 

回答

2

它看起來像你正試圖比較"1""Paper"並期待true

要麼使兩個比較true,即比較所述用戶輸入以"1"和計算機"Paper",或(相應地,另兩個值)轉換到"1""Paper"和響應直接比較。

0

personPlay可以是1,2或3. computerPlay可以是Paper,Rock或Scissors。

因此行f (personPlay.equals(computerPlay)){是沒有意義的。

0

如前所述你想一些比較字符串與該行

personPlay.equals(computerPlay) 

這裏personPlay是由人(讀爲String)和computerPlay輸入的數字是,你有字符串根據計算機的號碼設置即computerInt

易於修復方法就是改變上述的比較來檢查的computerInt

personPlay.equals(String.valueOf(computerInt)) 

字符串值,使數字進行比較(字符串)