Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Simple Increment in a Loop error

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Simple Increment in a Loop error

    It's probably something stupid, but I just can't see it as the same idea is working in the same code. Basically, it's a Rock, Paper, Scissors games - it ends when either the computer or user gets to 3, by declaring an int variable and incrementing it in the case of a win for either party. Loop then exits when get totalCompWin < 3 or totalUserWin < 3, I want the loop to exit as soon as it gets to 3 (so starts at 0, +1, +1 and next +1 exits loop. It exits fine once the user wins 3 times, but won't ever exit for the computer variable. Like I said, it's probably something stupid.

    import java.util.Scanner;
     
     
    public class rockPaperSci {
     
    	public static void main(String args[])
    	{
     
    		// 0 is rock, 1 is scissors, 2 is paper
    		Scanner input = new Scanner(System.in);
    		int userInput = 0, compGo, totalUserWin = 0, totalCompWin = 0;
    		double random;
     
     
     
    			do{
    				System.out.print("Enter 0 for Rock, 1 for Scissors, 2 for Paper: ");
    				userInput = input.nextInt();
     
    			//computer input
     
    			random = Math.random() * 100;
    			compGo = (int) random;
    			compGo = compGo % 3;
     
    			if (compGo == userInput)
    				System.out.print("Draw. Go again.\n");
     
    			else if (userInput == 0 && compGo == 1)
    			{
    				System.out.print("Rock beats Scissors! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput == 1 && compGo == 2)
    			{
    				System.out.print("Scissors beat Paper! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput == 2 && compGo == 0)
    			{
    				System.out.print("Paper beats Rock! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput >= 3)
    			{
    				System.out.print("Please read. Enter between 0 and 2, not 3 or higher!!!");
    			}
     
     
    			//for all non user wins, Computer wins. No need to show results really - can be added later on. After displaying Computers Wins, TotalCompWin gets incremented.
     
    			else 
    			{
    				System.out.print("Computer Wins! \n"); 
    				totalCompWin++;
    			}
     
     
     
    		}while (totalCompWin < 3 || totalUserWin < 3);
     
    //tried the below inside and outside while loop
     
    			if (totalUserWin == 3)
    				System.out.print("User Wins!!!!");
     
    			else if (totalCompWin == 3)
    				System.out.print("Rise of the Machines!!!! Computer whooped your candy ass!");
     
     
    //end
     
     
     
     
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Increment in a Loop error

    Are you sure it exits fine when the user wins 3 times? Take a look at your while loop:

    while(totalCompWin < 3 || totalUserWin < 3)

    That means the program will continue to loop while EITHER one is < 3. So the only way to exit the loop is for BOTH of them to be >= 3.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chrisob (May 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple Increment in a Loop error

    Yup, it definitely exits. Unless I'm being stupid, I thought that || meant OR so that the loop would continue while totalCompWin is less than 3 OR totalUserWin is less than 3. Once either hits 3, the loop should stop because one of them is 3. If I had && then the loop would continue until both hit 3, which is not what I want.

    Here's a sample console input. I just entered 1. As you can see, Computer Wins! came up 3 times but didn't terminate. As soon as the user won, it terminated.

    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Draw. Go again.
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Draw. Go again.
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Draw. Go again.
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Scissors beat Paper! User wins!
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Scissors beat Paper! User wins!
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Computer Wins!
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Computer Wins!
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Computer Wins!
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Draw. Go again.
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Draw. Go again.
    Enter 0 for Rock, 1 for Scissors, 2 for Paper: 1
    Scissors beat Paper! User wins!
    User Wins!!!!

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple Increment in a Loop error

    Actually, you are right. It's only exiting when both are 3. I thought that it should exit when either of the variables hit 3, whichever one, it doesn't matter.

    My idea was

    Do

    {

    < code >

    }
    while (numberOfUserWins is less than 3 OR numberOfComputerWins is less than 3)

    My understanding is that loop should terminate when either numberOfUserWins hits 3 or the other one does, not both. I would have thought (numberOFUserWins < 3 && numberOfCompWins < 3) would have given the current result

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Increment in a Loop error

    I suggest stepping through this with a debugger, or at the very least stepping through it in your head and using print statements to help you understand what's going on. When will that while loop evaluate to true? When will it evaluate to false? Throw in some test cases. What happens when both are 1? What happens when one is 2 and the other is 10? What happens when they are both 6?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple Increment in a Loop error

    Will do. It'll wait until later when I don't have a 3 year old trying to hang off me!

    Shall report back. This won't beat me!

  8. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Simple Increment in a Loop error

    Fixed, I think!

    import java.util.Scanner;
     
     
    public class rockPaperSci {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String args[])
    	{
     
    		// 0 is rock, 1 is scissors, 2 is paper
    		Scanner input = new Scanner(System.in);
    		int userInput = 0, compGo, totalUserWin = 0;
    		double random;
     
     
    		do
    		{
    				System.out.print("Enter 0 for Rock, 1 for Scissors, 2 for Paper: ");
    				userInput = input.nextInt();
     
    			//computer input
     
    			random = Math.random() * 100;
    			compGo = (int) random;
    			compGo = compGo % 3;
     
    			//end computer random digit of either 0, 1 or 2.
     
    			if (compGo == userInput)
    				System.out.print("Draw. Go again.\n");
     
    			else if (userInput == 0 && compGo == 1)
    			{
    				System.out.print("Rock beats Scissors! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput == 1 && compGo == 2)
    			{
    				System.out.print("Scissors beat Paper! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput == 2 && compGo == 0)
    			{
    				System.out.print("Paper beats Rock! User wins!\n"); totalUserWin++;
    			}
     
    			else if (userInput >= 3)
    			{
    				System.out.print("Please read. Enter between 0 and 2, not 3 or higher!!!");
    			}
     
     
    			//for all non user wins, Computer wins. No need to show results really - can be added later on. After displaying Computers Wins, TotalCompWin gets incremented.
     
     
     
    			else 
    			{
    				System.out.print("Computer Wins! \n"); 
    				totalUserWin--;
    			}
     
     
     
    		}
    		while (totalUserWin <= 2 ^ totalUserWin <= -3);
     
    //tried the below inside and outside while loop
     
    			if (totalUserWin > totalCompWin)
    				System.out.print("User Wins!!!!");
     
    			else 
    				System.out.print("Rise of the Machines!!!! Computer whooped your candy ass!");
     
     
    //end
     
     
     
     
    	}
     
    }

    Just used one variable for tracking score and an EXCLUSIVE OR (^) rather than an INCLUSIVE OR (||) in the loop. It kicks out if the user gets to 3 or the computer gets to -3!

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Simple Increment in a Loop error

    Are you sure that works? I really wouldn't expect it to. I think you were closer the first time around, and you're overcomplicating things now.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. One of the most simple loop awnsers
    By Firestar912 in forum Loops & Control Statements
    Replies: 8
    Last Post: May 7th, 2012, 07:13 PM
  2. [SOLVED] WHILE LOOP USING || (or) SIMPLE PROBLEM
    By SPACE MONKEY in forum Loops & Control Statements
    Replies: 3
    Last Post: May 7th, 2012, 12:23 AM
  3. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  4. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 04:31 AM
  5. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM