简体   繁体   中英

how to subtract from a for loop in java

i am making a rock paper scissors game in java for collage and was wondering how to subtract 1 from a for loop.

the full code works but if some one enters a invalid number (lower then 1 or higher then 3) my code asks them to reenter a number(1, 2, 3) but the for loop counts it as a loop so i end up with less moves.

i need to change something in the last "else if" but i cant figure it out

could some one point me in the right direction?
thanks.

the full code is this:

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("");

        }
    }
}

It is a scope issue. You are declaring a new variable gameLoop in your for loop, which hides the variable gameLoop that has been declared at the beginning of your class.

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

A quick and easy solution would be to just omit the 'int' in the for-loop:

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

Now, when you decrement it in the else-branch of your playerMoves() method, it should be noticed by the for-loop.

You could look towards keeping that while loop in main which checks if the playerHand is < 1 or > 3, and inside it, validate until the user inputs a valid number. The below is a sample idea of what you can look towards doing. It is not perfect.

//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.

I personally find validating here to be more convenient than validating in the playerMoves method, which is a bit long already as it is.

Alternatively, if you want to subtract the invalid guess from the for loop, you can do a gameLoop-- in the for loop for invalid guesses (and not execute the method by doing something like a continue after decrementing gameLoop ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM