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: RockPaperScissors

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile RockPaperScissors

    hi good evening.
    I have a problem upon compiling with my codes.this is for the game rock, paper and scissors..the problem is: Write a program that will allow user to input character: r-rock/ s-scissors/p-paper. and display the remarks...such as when player1=='r' && player2=='R' it will output "DRAW!. Nobody wins". and if (player1=='r' && player2== 's') the output is: "rock covers scissors". . . . and so on and so forth..and then, the program will only stop if the user wants to stop it. like "Do you wish to continue? press Y-yes and N-no.

    so here's my code and the problem when i compile it is...,
    it will not go back to the 2 round.... wooooooaaah!! please help me figure this out..Thank you and have a nice day!


     
    import java.util.*;
     
    public class PaperRockScissors {
    	public static void main (String[]args){
    	char p1, p2, repeat;
     
    	Scanner console= new Scanner(System.in);
     
    	System.out.print("Player1 turn: ");
    	p1= console.next().charAt(0);
     
    	System.out.print("Player2 turn: ");
    	p2= console.next().charAt(0);
     
    	do{
     
    			if (((p1 == 'r') || (p1== 'R')) && ((p2== 'r') || (p2== 'R')))
    			System.out.print("DRAW! Nobody wins.");
     
    			else if (((p1 == 'p') || (p1== 'P')) && ((p2== 'p') || (p2== 'P')))
    			System.out.print("DRAW! Nobody wins.");
     
    			else if (((p1 == 's') || (p1== 'S')) && ((p2== 's') || (p2== 'S')))
    			System.out.print("DRAW! Nobody wins. ");
     
    	//for scissors
    			else if (((p1 == 's') || (p1== 'S')) && ((p2== 'p') || (p2== 'P')))
    			System.out.print("Scissors cut the paper. Player1 wins!");
    			else if (((p1 == 'p') || (p1== 'P')) && ((p2== 's') || (p2== 'S')))
    			System.out.print("Scissors cut the paper. Player2 wins!");
     
    	//for paper
    			else if (((p1 == 'p') || (p1== 'P')) && ((p2== 'r') || (p2== 'R')))
    			System.out.print("Paper covers rock. Player1 wins!");
    			else if (((p1 == 'r') || (p1== 'R')) && ((p2== 'p') || (p2== 'P')))
    			System.out.print("Paper covers rock. Player2 wins!");
     
    	//for rock
    			else if (((p1 == 'r') || (p1== 'R')) && ((p2== 's') || (p2== 'S')))
    			System.out.print("Rock breaks scissors. Player1 wins!");
    			else if (((p1 == 's') || (p1== 'S')) && ((p2== 'r') || (p2== 'R')))
    			System.out.print("Rock breaks scissors. Player2 wins!");
     
     
    			//repeat
    			System.out.println("\nDo you wish to continue the game? \n Y-yes N-no: ");
    			repeat= console.next().charAt(0);
    	}
     
    	while((repeat=='y') || (repeat== 'Y'));
     
    	}
    }
    Last edited by helloworld922; May 6th, 2010 at 10:39 AM.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: RockPaperScissors

    You need to put the player 1 and player 2 turns inside the loop, otherwise the first values entered never get replaced.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RockPaperScissors

    thanks.. already solved