2015-11-29 71 views
0

我正在用java編寫一個岩石紙剪刀遊戲拼貼,並想知道如何從for循環中減去1。如何從java中的for循環中減去

完整的代碼工作,但如果有人輸入一個無效的數字(低於1或更高,然後3)我的代碼要求他們重新輸入一個數字(1,2,3) 但for循環計算它爲一個循環所以我最終以較少的舉動。

我需要改變一些東西在最後的「否則,如果」但我不能弄明白

可能有人點我在正確的方向?
謝謝。

完整的代碼是這樣的:

import java.io.*; 
import java.util.Random; 

public class RockPaperScissors { 
    static int loss = 0; 
    static int win = 0; 
    static int tie = 0; 
    int draw; 
    static int playerHand; 
    static int compHand; 
    int gameLoop; 

    public void playerMoves() { 
    if (playerHand == compHand){ //if both hands (player and computer) are the same 
     System.out.println("Draw, your picked " + playerHand + " and the computer picked " + compHand); 
     tie++; // add 1 to tie score 
    } 
    else if (playerHand == 1 && compHand == 2){ // if player picks Rock and computer picks paper 
     System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, You lose"); 
     loss++; // add 1 to loss score 
    } 
    else if (playerHand == 1 && compHand == 3){ // if player picks rock and computer scissors 
     System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, You win!"); 
     win++; // add 1 to win score 
    } 
    else if (playerHand == 2 && compHand == 1){ //if player picks paper and computer picks rock 
     System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, you win!"); 
     win++; // add 1 to win score 
    } 
    else if (playerHand == 2 && compHand == 3){ // if player picks paper and computer scissors 
     System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you lose!"); 
     loss++; // add 1 to loss score 
    } 
    else if (playerHand == 3 && compHand == 1){ // if player picks scissors and computer rock 
     System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, you lose!"); 
     loss++; // add 1 to loss score 
    } 
    else if (playerHand == 3 && compHand == 2){ // if player picks scissors and computer paper 
     System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you win!"); 
     win++; // add 1 to win score 
    } 
    else if (playerHand < 1 || playerHand > 3) { 
     System.out.println(playerHand + " is not a valid number. Try again...");// if not valid number ask again. 
     gameLoop = gameLoop - 1; // subtract 1 from gameLoop 
    } 
    else { 
     System.out.println("Great job, you broke it..."); 
    } 
    } 

    public static void main(String[] args) throws IOException { 
    BufferedReader br = new BufferedReader(
      new InputStreamReader(System.in)); 

    System.out.println("Welcome to Rock Paper Scissors"); 
    System.out.println("Lets play ten games and see if you can outsmart the computer!"); 

    for (int gameLoop = 0; gameLoop < 10; gameLoop++) { // a for loop to keep the game running 10 times 
     Random randomNumber = new Random(); // create a new random number everytime 


     compHand = (int) randomNumber.nextInt(3); // generate a random number for the computer (compHand) 
     compHand++; 

//  while (playerHand < 1 || playerHand > 3) { 
//   System.out.println(playerHand + " is not a valid move. Try again..."); 
      System.out.println("Rock(1), Paper(2), or Scissors(3) Please enter the number"); 
      RockPaperScissors draw = new RockPaperScissors(); 
      RockPaperScissors.playerHand = Integer.parseInt(br.readLine()); 
      draw.playerMoves(); // go to public void playerMoves and use that. 

     System.out.println("the score is: " + win + " Games won. " + loss + " Games lost. " + tie + " Games tie."); // print out the game score at the end of every game 
     System.out.println(""); 

     } 
    } 
} 

回答

2

這是一個範圍問題。你正在for循環中聲明一個新的變量gameLoop,它隱藏了在你的類的開始處聲明的變量gameLoop。

public class RockPaperScissors { 
... 
    int gameLoop; // 1. variable with name gameLoop declared 

... 
    // 2. variable with name gameLoop declared; hides 1. declaration 
    for (int gameLoop = 0; gameLoop < 10; gameLoop++) { 
      ^this is not the same variable as above 

一個快速簡便的解決方案是隻省略「廉政」在for循環:

for (gameLoop = 0; gameLoop < 10; gameLoop++) { 

現在,當您在playerMoves的其他分支遞減它()方法,它應該被for循環注意到。

+0

這工作就像一個魅力,謝謝。現在要清理一些東西,看看我能否縮小它。 – jaylow

0

你可以看一下對保持該while環路main它檢查是否playerHand是< 1> 3,而在它裏面,驗證,直至用戶輸入一個有效的數。下面的 是您可以看到的一個示例。這並不完美。

//Taking in the first input 
System.out.println("Rock(1), Paper(2), or Scissors(3) Please enter the number"); 
RockPaperScissors draw = new RockPaperScissors(); 
RockPaperScissors.playerHand = Integer.parseInt(br.readLine()); 

//Validating the input repeatedly until it is valid 
while (playerHand < 1 || playerHand > 3) { 
    System.out.println(playerHand + " is not a valid move. Try again..."); 
    System.out.println("Rock(1), Paper(2), or Scissors(3) Please enter the number"); 
    RockPaperScissors.playerHand = Integer.parseInt(br.readLine()); 
} 

//Continue with your program 
draw.playerMoves(); // go to public void playerMoves and use that. 

我個人覺得這裏確認是不是在playerMoves方法,這是一個有點已久的,因爲它是驗證更方便。

另外,如果你想從for環減去無效的猜測,你可以在for循環無效猜測做了gameLoop--(而不是遞減gameLoop後做一些像continue執行方法)。

+0

這給了我一些問題draw.playerMoves();我無法弄清楚,我知道playerMovement有點長,我希望我稍後可以清理它,這只是我第一次嘗試它。 – jaylow

+0

我在while循環中刪除了'RockPaperScissors draw = new RockPaperScissors();'的重複初始化,它現在對我來說工作正常(剛測試過;沒有機會昨天測試它)。對於那個很抱歉。至少你得到了另一個解決方案。此外,有[很多例子](https://stackoverflow.com/questions/14136721/converting-many-if-else-statements-to-a-cleaner-approach)的方式來清理如果 - 否則看起來更整潔[這裏也是](https://stackoverflow.com/questions/1199646/long-list-of-if-statements-in-java)。 – Foleosy