2013-06-22 83 views
0

我是Java的新手,並且創建了某種迷你遊戲,並且我希望玩家選擇是否要再次玩,我嘗試更改我的啓動布爾變量爲靜態類型並添加一些代碼行,但代碼似乎不起作用,每次我玩遊戲時,它都會問我是否要重播,但問題是即使我最後在控制檯鍵入「是」,遊戲似乎不會重新啓動。任何人都可以幫助我,我會非常感激,謝謝!字符串輸入在if-else語句中不起作用

import java.util.Random; 
import java.util.Scanner; 

//Rocks, papers, scissors game complied by William To. 
public class RockPaperScissor { 
static int gamePlays = 0; 
static boolean start = true; 
public static void main(String[] args) { 
    while (start){ 
     @SuppressWarnings("resource") 
     Scanner userInput = new Scanner(System.in); 
     Random comOutput = new Random(); 
     System.out.println("Do you choose rock, paper or scissor?"); 
     String userChoice = userInput.nextLine(); 
     int comChoice = comOutput.nextInt(2); 
     switch (userChoice){ 

     case "rock": 
      if(comChoice == 0){ 
       System.out.println("I choose rock too! That's a draw!"); 
      } else if(comChoice == 1){ 
       System.out.println("I choose paper! I win!"); 
      } else { 
       System.out.println("I choose scissor! You win!"); 
      } 
      break; 

     case "paper": 
      if(comChoice == 0){ 
       System.out.println("I choose rock! You win!"); 
      } else if(comChoice == 1){ 
       System.out.println("I choose paper too! That's a draw!"); 
      } else { 
       System.out.println("I choose scissor! I win!"); 
      } 
      break; 

     case "scissor": 
      if(comChoice == 0){ 
       System.out.println("I choose rock! I win!"); 
      } else if(comChoice == 1){ 
       System.out.println("I choose paper! You win!"); 
      } else { 
       System.out.println("I choose scissor too! That's a draw!"); 
      } 
      break; 
     default : 
      System.out.println("I don't think that's an option."); 
      break; 
     } 
     gamePlays++; 
     System.out.println("You've played " + gamePlays + " games."); 

       //BELOW IS THE PART I want to ask, what's wrong? 

     System.out.println("Do you want to play again?"); 
     String playAgain = userInput.next(); 
     if (playAgain == "yes"){ 
      System.out.println("Restarting game..."); 
      start = true; 
     } else if (playAgain == "no") { 
      System.out.println("Quitting game."); 
      start = false; 
     } 
    } 
} 
} 

Eclipse中的打印輸出:

Do you choose rock, paper or scissor? 
rock **//my input**  
I choose paper! I win! 
You've played 1 games. 
Do you want to play again?  
yes **//my input**  
Do you choose rock, paper or scissor? 
paper **//my input**  
I choose paper too! That's a draw! 
You've played 2 games. 
Do you want to play again? 
no **//my input** 
Do you choose rock, paper or scissor? 
+0

[我如何在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –

+6

NOOOOOOOOOOOOOOOOOOOOOOOOO不是可能重複' ==''再次:_( – Maroun

+1

第100次我看到這個問題 –

回答

3

字符串比較需要String#equals而不是==。所以這個:

if (playAgain == "yes"){ 
    System.out.println("Restarting game..."); 
    start = true; 
} else if (playAgain == "no") { 
    System.out.println("Quitting game."); 
    start = false; 
} 

有可能成爲這樣的:

if (playAgain.equals("yes")){ 
    System.out.println("Restarting game..."); 
    start = true; 
} else if (playAgain.equals("no")) { 
    System.out.println("Quitting game."); 
    start = false; 
} 

爲了正常工作。

不過,我建議使用String#equalsIgnoreCase,因爲equals,比較是大小寫敏感的,所以如果用戶輸入即YesyEsplayAgain.equals("yes")將返回false

UPDATE

由於Maroun Maroun說,最好用

"yes".equals(playAgain) 

and

"no".equals(playAgain) 

因爲如果playAgain是null它不會拋出NullPointerException

+2

+1。雖然我更喜歡' 「是」.equals(playAgain)'以防''playAgain'爲'null',它不會是NPE。 – Maroun

+0

@MarounMaroun對!編輯, 謝謝! – BackSlash

+2

@MarounMaroun我總是想知道爲什麼人們總是把字符串字面先放。現在我知道,謝謝! –

3

問題可能是這條線

if (playAgain == "yes") 

(後來因爲沒有一個)。

Java中的字符串是對象。這意味着兩個字符串可能具有相同的內容,即包含相同的文本,但就==而言仍然不是「相同的」。比較兩個字符串,看看他們是否包含相同的文本,使用這樣的:

if (playAgain.equals("yes")) 
+0

謝謝你們!我真的很感謝你的幫助:) –

+0

謝謝!我非常感謝你的幫助! :D –

0

你需要使用String.equals()法和比較字符串不==

equals方法比較的字符串和==檢查的價值不管這兩個對象是否指向相同的對象。

+0

_此外,你需要在開關塊中的每個情況之後放置break語句,否則它不會跳出開關塊並執行下面所有的開關case_在OP發佈的代碼中,他**正在做**那。 – BackSlash

+0

@ BackSlash無法注意..謝謝:)現在編輯答案 –

+0

謝謝你們兩位! :D –

4

嘗試:

if ("yes".equals(playAgain)) { 
     System.out.println("Restarting game..."); 
     start = true; 
    } else if ("no".equals(playAgain)) { 
     System.out.println("Quitting game."); 
     start = false; 
    } 

String#equals()檢查字符串的字符平等,==檢查存儲器參考。

+3

+1。如果'playAgain'爲'null',那麼你不能得到NPE。 – Maroun

1

問題與String比較的完成方式有關。

playAgain == "yes",該檢查將檢查對象是否相等。

但是在這種情況下,對象的值需要進行比較,下面的代碼將適用於您。

playAgain.equals("yes") 
+0

感謝您的回答! ^^ –