如何从java中的for循环中减去

时间:2015-11-29 19:28:55

标签: java loops

我在java中制作一个用于拼贴的石头剪刀游戏,并想知道如何从for循环中减去1。

完整代码有效,但如果有人输入无效数字(低于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 个答案:

答案 0 :(得分: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

快速简便的解决方案是省略&#39; int&#39;在for循环中:

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

现在,当你在playerMoves()方法的else-branch中递减它时,它应该被for循环注意到。

答案 1 :(得分:0)

您可以考虑在while中保留main循环,以检查playerHand是否&lt; //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. 1或&gt; 3,在其中,验证直到用户输入有效数字。该 下面是您可以看到的样子。它并不完美。

playerMoves

我个人认为在这里验证比在for方法中验证更方便,因为它已经有点长了。

或者,如果您想从gameLoop--循环中减去无效猜测,可以在for循环中执行continue进行无效猜测(而不是通过执行方法执行该方法递减gameLoop后的li之类的内容。