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 3 of 3

Thread: how come this program only loops twice? wheres my mistake?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post how come this program only loops twice? wheres my mistake?

    hey guys so i been asked to write this rock paper scissors program. and it should loop until whenever either player or computer reaches 3 wins. however, it only loops twice for some reason here's my code and thanks in advance.
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    class RockPaperScissors
    {
    	public static void main(String args[])
    	{
    		int compWin = 0;
    		int userWin = 0;
    		Scanner input = new Scanner(System.in);
    		JOptionPane.showMessageDialog(null,
    		"ROCK - PAPER - SCISSORS" + " \n Whoever wins more than two times is the winner!");
    		do
    		{
    			int totalRound = 1;
    			String s = JOptionPane.showInputDialog(null,
    			"Computer's Move: X" + "\n Your Move: ",
    			"Scissor-Rock-Paper",
    			JOptionPane.QUESTION_MESSAGE);
    			int player = Integer.parseInt(s);
    			if ( player < 3)
    			{
     
     
    				int computer = (int)(Math.random() * 3);
    				if (computer == player)
    				{
    					JOptionPane.showMessageDialog(null, "it's a tie make your move again");
    				}
    					else if ( computer == 0 && player == 2 )
    					{
    						JOptionPane.showMessageDialog(null, "scissor0 cuts paper2 computer wins");
    						compWin++;
    					}
    					else if ( computer == 0 && player == 1)
    					{
    						JOptionPane.showMessageDialog(null, " rock1 beats scissor0 player wins");
    						userWin++;
    					}
    					else if ( computer == 1 && player == 0)
    					{
    						JOptionPane.showMessageDialog(null, " rock1 beats scissor0 computer wins");
    						compWin++;
    					}
    					else if ( computer == 1 && player == 2)
    					{
    						JOptionPane.showMessageDialog(null, " paper2 beats rock1 player wins");
    						userWin++;
    					}
    					else if ( computer == 2 && player == 0)
    					{
    						JOptionPane.showMessageDialog(null, " scissor0 cuts paper2 player wins");
    						userWin++;
    					}
    					else if ( computer == 2 && player == 1)
    					{
    						JOptionPane.showMessageDialog(null, "paper2 beats rock1 computer wins");
    						compWin++;
    					}
     
    					totalRound++;
    			}
     
    		   	else
    		    {
    		   		JOptionPane.showMessageDialog(null, "Sorry! No such choice. Please enter (0) for Scissor, (1) for Rock, and (2) for paper");
    			}
     
    			JOptionPane.showInputDialog(null,
    			"Computer's Move: X" + "\n Your Move: ",
    			"Scissor-Rock-Paper",
    			JOptionPane.QUESTION_MESSAGE);
    	}while (compWin > 2 || userWin > 2);
    }
    }
    Last edited by Rainy; September 2nd, 2011 at 11:40 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    25
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: how come this program only loops twice? wheres my mistake?

    Quote Originally Posted by Rainy View Post
    hey guys so i been asked to write this rock paper scissors program. and it should loop until whenever either player or computer reaches 3 wins. however, it only loops twice for some reason here's my code and thanks in advance.
    do
    {...
    }
    while (compWin > 2 || userWin > 2);
    }
    your loop doesn't loop even one times you think that it loops twice because you used "do while" and you used two Input JOptionpanes.
    while(compWin > 2 || userWin > 2)
    means loop until compWin or userWin bigger than 2
    use while(compWin<3 && userWin<3)
    this will loop until both compWin and userWin<3

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

    Rainy (September 3rd, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how come this program only loops twice? wheres my mistake?

    Quote Originally Posted by serdar View Post
    your loop doesn't loop even one times you think that it loops twice because you used "do while" and you used two Input JOptionpanes.
    while(compWin > 2 || userWin > 2)
    means loop until compWin or userWin bigger than 2
    use while(compWin<3 && userWin<3)
    this will loop until both compWin and userWin<3
    OMG thank you so much!!! i feel so dumb right now, shouldn't make this kind of mistakes haha. thanks again!

Similar Threads

  1. program loops never ending
    By Ccogh05 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 24th, 2011, 01:42 AM
  2. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM
  3. Program without loops and recursion
    By jayaram in forum Algorithms & Recursion
    Replies: 7
    Last Post: April 1st, 2010, 09:02 AM
  4. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM
  5. wheres the thread about inverted String?
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 29th, 2009, 11:13 AM