Kody_06
Kody_06

Reputation: 315

Can I make an exception to the counter in a for loop?

I'm trying to make a program to play rock, paper, scissors with a computer. However, part of the assignment is having the player choose the amount of rounds to play with the computer. If there is a tie, the round is not supposed to count towards the game.

My code looks like this -- I'm more focused on the for part of the program:

int gamePlays = 0;

for (int = 0; i < gamePlays; i++)
{
 // If, else if, and else statements to make the game possible. 
 // Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
  if (userChoice.equals ("scissors"))
  {
      System.out.println("Rock beats scissors, computer wins.");
  }
  else if (userChoice.equals ("paper"))
  {
      System.out.println("Paper beats rock, computer wins.");
  }
  else 
  {
      System.out.println("You both chose rock! It's a tie.");
  }
// The rest would else if cpuChoice.equals "paper" and else. 
}

How do I exclude the last else statement when it's a tie from the counter? I've wrestled with this for a couple hours and can't find any solutions.

Upvotes: 0

Views: 125

Answers (4)

Andreas
Andreas

Reputation: 159086

First, your message it wrong. When cpuChoice.equals("rock") and userChoice.equals("paper"), then correct message is:

Paper beats rock, player wins.

Also, if gamePlays = 0, there will be no game. ;-)

Since you probably want to keep score too, just use the scores:

int cpuWins = 0;
int userWins = 0;
while (cpuWins + userWins < gamePlays) {
    if (cpuChoice.equals("rock")) {
        if (userChoice.equals("scissors")) {
            System.out.println("Rock beats scissors, computer wins.");
            cpuWins++;
        } else if (userChoice.equals("paper")) {
            System.out.println("Paper beats rock, player wins.");
            userWins++;
        } else {
            System.out.println("You both chose rock! It's a tie.");
        }
    } else ...
        ...
    }
}
if (cpuWins > userWins)
    System.out.println("Computer is champion.");
else if (cpuWins < userWins)
    System.out.println("Player is champion.");
else
    System.out.println("There is no champion.");

Upvotes: 0

AJ.
AJ.

Reputation: 4534

Instead of for I suggest while

int i = 0; 
while (i < gamePlays)
{
 // If, else if, and else statements to make the game possible. 
 // Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
  if (userChoice.equals ("scissors"))
  {
      System.out.println("Rock beats scissors, computer wins.");
      i++; // increment only when there is result
  }
  else if (userChoice.equals ("paper"))
  {
      System.out.println("Paper beats rock, computer wins.");
      i++; // increment only when there is result
  }
  else 
  {
      System.out.println("You both chose rock! It's a tie.");
     // don't increment if no result
   }
// The rest would else if cpuChoice.equals "paper" and else. 
}

}

Upvotes: 3

Chit Khine
Chit Khine

Reputation: 850

if that happens, just put

i--; 

for that statement. Then the round would not be included. For your question.

for (int = 0; i < gamePlays; i++)
{
 // If, else if, and else statements to make the game possible. 
 // Here is an example of part of the code:
if (cpuChoice.equals ("rock"))
{
  if (userChoice.equals ("scissors"))
  {
      System.out.println("Rock beats scissors, computer wins.");
  }
  else if (userChoice.equals ("paper"))
  {
      System.out.println("Paper beats rock, computer wins.");
  }
  else 
  {
      System.out.println("You both chose rock! It's a tie.");
      i--;
  }
// The rest would else if cpuChoice.equals "paper" and else. 
}

This will make the 'i' to remain the same since your 'i' will be incremented after the loop.

Upvotes: 1

geoff
geoff

Reputation: 2276

Your for loop creates a variable i which you can modify in the body of the for loop. If you want the code to have the effect of not incrementing the counter when the user ties with the CPU, then decrement i where you need it:

int gamePlays = 0
for (int i = 0; i < gamePlays; i++) {
   ...
   else {
     // this is where you handle ties
     System.out.println("You tied with CPU");
     i--; // decrement the counter
   }
   ...
}

Upvotes: 2

Related Questions