2014-03-18 60 views
1

我正在創建一個岩石,紙張,剪刀遊戲以嘗試我的Java技能。在我的代碼中,我有一個循環循環,直到贏家贏得或輸掉他們輸入的循環數。這部分不起作用。它直接進入邏輯語句,即使沒有完成循環,它也在while循環之外。Java程序不環繞while循環 - 直接轉到if語句

代碼:

package rps_game; 
// Rock, Paper, Scissors game 
// 

import java.util.*; 

public class rock_paper_scissors { 
    public static void main(String args[]) { 

     Scanner input = new Scanner(System.in); 
     Scanner lineInput = new Scanner(System.in); 
     Random rand = new Random(); 

     String choices[] = { "rock", "paper", "scissors" }; 

     int winRounds = 0; 
     boolean running = true; 

     int rounds = 0; 
     int wins = 0; 
     int loses = 0; 

     String yourChoice = ""; 
     String compChoice = ""; 
     while (running = true) { // main loop 

      try { 
       System.out 
         .println("Enter the amount of rounds you want to play: "); 
       rounds = input.nextInt(); // gets input 
       winRounds = (rounds/2) + 1; 
       System.out.println("You are playing best of " + winRounds 
         + " out of " + rounds); 

       //running = false; // breaks off from loop 

       while (wins < winRounds && loses < winRounds) { // this while loop 
        System.out.println("Enter either Rock, Paper or Scissors: "); 

        yourChoice = lineInput.nextLine(); 
        yourChoice= yourChoice.toLowerCase(); 
        if (Arrays.asList(choices).contains(yourChoice)) { // what use entered 
         break; 
        } else { 
         System.out.println("You did not enter either Rock, Paper or Scissors."); 
         System.exit(0); 
        } 

        compChoice = choices[rand.nextInt(choices.length)]; 
        System.out.println("ROCK.. PAPER.. SCISSORS!"); 
        System.out.println("You entered: " + yourChoice + " and the computer entered: " + compChoice); 

        if (yourChoice.equals(compChoice)){ 
         System.out.println("\nIt's a draw!"); 
        }else if(yourChoice.equals("rock") && compChoice.equals("paper")){ 
         System.out.println("\nYou lose!"); 
         loses++; 
        }else if(yourChoice.equals("paper") && compChoice.equals("rock")){ 
         System.out.println("\nYou win!"); 
         wins++; 
        }else if(yourChoice.equals("scissors") && compChoice.equals("paper")){ 
         System.out.println("\nYou win!"); 
         wins++; 
        }else if(yourChoice.equals("paper") && compChoice.equals("Scissors")){ 
         System.out.println("\nYou lose!"); 
         loses++; 
        }else if(yourChoice.equals("rock") && compChoice.equals("scissors")){ 
         System.out.println("\nYou win!"); 
         wins++; 
        }else if(yourChoice.equals("scissors") && compChoice.equals("rock")){ 
         System.out.println("\nYou lose!"); 
         loses++; 
        }else{ 
         break; 
        } 

       } 
       System.out.println("\nGAME FINISHED!"); 

       if (wins > loses){ 
        System.out.println("\nYOU WIN THE GAME!!"); 
       }else if(wins < loses){ 
        System.out.println("\nYOU LOST THE GAME!! :("); 
       }else{ 
        System.out.println("It's a draw!!"); 
        System.exit(0); 
       } 

      } catch (Exception e) { // if error pops up 
       System.out.println("You DID NOT enter a WHOLE NUMBER."); 
       break; 
      } 

     } 

     input.close(); 
     lineInput.close(); 
    } 
} 

爲什麼沒有像循環它應該是?我真的不知道這一點

回答

4

的問題是你的break語句

if (Arrays.asList(choices).contains(yourChoice)) { // what use entered 
    break; 
} 

break使你退出循環,我不認爲你想退出循環,當你選擇了正確的輸入。


中,事實上,因爲你也有該else塊內System.exit命令,沒有你的內心,而循環將這兩行後執行的,不管是什麼。

if (Arrays.asList(choices).contains(yourChoice)) { // what use entered 
    break; 
} else { 
    System.out.println("You did not enter either Rock, Paper or Scissors."); 
    System.exit(0); 
} 

我會建議擺脫break聲明,並變更System.exit(0);continue,這將直接跳過到循環的下一次迭代。如果你這樣做,你可能也可以重新工作if邏輯,因爲只有1個條件。

+0

@KubaSpatny是的。打破內在,同時是OP描述的問題 –

+0

是的抱歉,我在我的手機上看着它,並認爲'如果'是在外面以後.. –

+0

是的!非常感謝。在Java和其他語言的while循環方面我還沒有太多的經驗。但是謝謝你! – user3092741

2

while (running = true)應該while (running == true)

編輯:考慮Yoda Conditions,以此來避免這樣做。

+3

或者只是'while(running)'。 :) – asteri

+2

這是一個很好的提示,但不是問題 – Brian

0

像@大衛·埃爾曼說,你需要改變你的running = truerunning == true或只是while(running),你在這個邏輯,如果是錯誤的說法:

if (Arrays.asList(choices).contains(yourChoice)) { // what use entered 
    break; 
} else { 
    System.out.println("You did not enter either Rock, Paper or Scissors."); 
    System.exit(0); 
} 

你不想break當你進入正確的輸入。一個適當的if語句應該是:

if (!(Arrays.asList(choices).contains(yourChoice))) 
{ 
    System.out.println("You did not enter either Rock, Paper, or Scissors."); 
    running = false; 
    break; 
} 
else 
{ 
    // the rest of the playing code 
} 

這假定你想要優雅地退出。它應該打印出「這是一個平局」。並以這種方式清理任何資源......如果這就是你想要的。

+0

好的答案,這樣做比使用'break;'更好 –