2017-05-13 29 views
-1

我想編碼岩石,紙,剪刀,蜥蜴,Spock。我的代碼已附加,循環不斷重複。我相信花括號是錯誤的,但我不知道哪裏沒有得到錯誤信息。如何解決循環問題?這是括號的地方,但我不知道他們應該放在哪裏

public class RockPaperScissors_v2 { 

    public static void main(String[] args) { 

     //scanner 
     Scanner inputReader = new Scanner(System.in); 
     //randomizer for comp 
     Random rand = new Random(); 

     //variables 
     int rock = 1; 
     int paper = 2; 
     int scissors = 3; 
     int lizard = 4; 
     int spock = 5; 
     int round; 
     int choice; 
     int compChoice = 1; 
     int countOfWins = 0; 
     int countOfLosses = 0; 
     int countOfTies = 0; 

     //ask how many rounds to play; max 10 --> if more than 10, print error message and quit. 
     do { 
      System.out.println("How many rounds do you want to play?"); 
      //output 
      round = inputReader.nextInt(); 
      if (round < 1 || round > 10) { 
       System.out.println("Error Message: Please type a number between 1 and 10."); 
       return; 
      } 
     } while (round != 0); 

     System.out.println("Please select a number from the follow:"); 
     System.out.println("1) Rock" 
       + "\n 2) Paper" 
       + "\n 3) Scissors" 
       + "\n 4) Lizard" 
       + "\n 5) Spock"); 

     compChoice = rand.nextInt(5 - 2 + 2) + 1; 

     choice = inputReader.nextInt(); 
     //if statements for all scenarios 
     if (compChoice == 1) { 
      if (choice == 1) { 
       System.out.println("Tie game."); 
       countOfTies++; 
      } else if (choice == 2) { 
       System.out.println("Rock gets covered by paper. You win!"); 
       countOfWins++; 
      } else if (choice == 3) { 
       System.out.println("Rock crushes scissors. You lose."); 
       countOfLosses++; 
      } else if (choice == 4) { 
       System.out.println("Rock crushes lizard. You lose."); 
       countOfLosses++; 
      } else if (choice == 5) { 
       System.out.println("Spock vaporizes rock. You win!"); 
       countOfWins++; 
      } 
     } else if (compChoice == 2) { 
      if (choice == 1) { 
       System.out.println("Paper covers rock. You lose."); 
       countOfLosses++; 
      } else if (choice == 2) { 
       System.out.println("Tie game."); 
       countOfTies++; 
      } else if (choice == 3) { 
       System.out.println("Scissors cuts paper. You win!"); 
       countOfWins++; 
      } else if (choice == 4) { 
       System.out.println("Lizard eats paper. You win!"); 
       countOfWins++; 
      } else if (choice == 5) { 
       System.out.println("Paper disproves Spock. You win!"); 
       countOfWins++; 
      } 
     } else if (compChoice == 3) { 
      if (choice == 1) { 
       System.out.println("Rock crushes scissors. You lose."); 
       countOfLosses++; 
      } else if (choice == 2) { 
       System.out.println("Scissors cuts paper. You lose."); 
       countOfLosses++; 
      } else if (choice == 3) { 
       System.out.println("Tie game."); 
       countOfTies++; 
      } else if (choice == 4) { 
       System.out.println("Scissors decapitates lizard. You lose."); 
       countOfLosses++; 
      } else if (choice == 5) { 
       System.out.println("Spock smashes scissors. You win!"); 
       countOfWins++; 
      } 
     } else if (compChoice == 4) { 
      if (choice == 1) { 
       System.out.println("Rock crushes lizard. You win!"); 
       countOfWins++; 
      } else if (choice == 2) { 
       System.out.println("Lizard eats paper. You lose."); 
       countOfLosses++; 
      } else if (choice == 3) { 
       System.out.println("Scissors decapitates lizard. You win!"); 
       countOfWins++; 
      } else if (choice == 4) { 
       System.out.println("Tie game."); 
       countOfTies++; 
      } else if (choice == 5) { 
       System.out.println("Lizard poisons Spock. You lose."); 
       countOfLosses++; 
      } 
     } else if (compChoice == 5) { 
      if (choice == 1) { 
       System.out.println("Rock crushes lizard. You lose."); 
       countOfLosses++; 
      } else if (choice == 2) { 
       System.out.println("Lizard eats paper. You win!"); 
       countOfWins++; 
      } else if (choice == 3) { 
       System.out.println("Scissors decapitates lizard. You lose."); 
       countOfLosses++; 
      } else if (choice == 4) { 
       System.out.println("Lizard poisons Spock. You win!"); 
       countOfWins++; 
      } else if (choice == 5) { 
       System.out.println("Tie game."); 
       countOfTies++; 
      } 
     } 

     //print out wins, losses, and ties 
     System.out.println("Number of Wins:" + countOfWins); 
     System.out.println("Number of Losses: " + countOfLosses); 
     System.out.println("Number of Ties: " + countOfTies); 

     //ask user if wants to play again 
     System.out.println("Do you want to play again (Y/N)?"); 
     String playAgain = inputReader.next(); 
     if (playAgain.equals("Y")) { 
     } else { 
      System.out.println("Thank you for playing."); 
      System.exit(0); 
     } 
    } 
} 
+1

有很多花括號......嘗試製作方法和[mcve] –

回答

0

隨着while (round != 0);當你插入0,這似乎是錯誤的循環將唯一的出口,也因爲在這種情況下,你與return關閉程序將永遠不會發生。

我建議以除去return並用while (round < 1 || round > 10);改變while條件,這將終止僅當插入110之間的值的迴路。

相關問題