Jump to content

Hey I'm writing a rock, paper scissors program for school

I have the user enter a number of rounds to play, but we have to make sure its odd.

This While loop works, but no matter what value you put in the first time, it spits out the "invalid selection" what am I doing wrong here? 

 


/*************
*Operation5.java
*Keyan north
*
*This program runs a game of rock, paper, scissors!
*************/
import java.util.Scanner;
import java.util.Random;

public class Operation5
{
   public static void main (String[] args)
   {
      Scanner in = new Scanner(System.in);
      Random rng = new Random();
      
      int totalRounds = 0; //total number of rounds
      int userScore = 0; //the user's score
      int compScore = 0;//computer's score
      int round = 0; //the current round being played
      String userMove = ""; //user's 
      String compMove = "";
      String winner = "";
      
      
      //get number of rounds
      System.out.println("Welcome to Rock, Paper, Scissors!");
      System.out.println("Please chose a number of rounds:");
      totalRounds = in.nextInt();
      
      while(totalRounds % 2 == 0); //make sure number of rounds is odd
      {
         System.out.println("invalid selection. Number of rounds must be odd. Try again:");
         totalRounds = in.nextInt();
      }
      
      System.out.println(totalRounds + " rounds selected. Lets Get Started.");
   }//end main
}//end Operation5

Link to comment
https://linustechtips.com/topic/737342-java-what-loop-do-i-use/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×