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

Thread: rock paper scissors

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default rock paper scissors

    I was wondering how I could make this code for rock paper scissors repeat itself 10x




     
    import java.util.Scanner;
    import java.util.Random; 
       public class Rock {
         public static void main (String [] args) {
     
           int scissor = 0;
           int rock = 1;
           int paper = 2;
     
     
         Random random = new Random(); 
     
                int computerNumber = random.nextInt(3);
                 Scanner input = new Scanner (System.in);
                  System.out.println (" Please enter a number 0, 1, or 2.");
                     int userNumber = input.nextInt();
                             if (userNumber > 2) {
     
    System.out.println("Incorrect value entered");
     
                             }
     
     
        System.out.println("Computer Value:" + computerNumber);   
     
           if (userNumber == computerNumber) {
     
    System.out.println("Its a draw");
     
    return;
     
    }
     
           if (userNumber == scissor) {
             if (computerNumber == paper) {
           System.out.println("User wins!");
           return;
     
             } else if (computerNumber == rock) {
               System.out.println ("Computer wins!");
               return;
             }
             main(null);
             } else if (userNumber == rock) {
               if (computerNumber == paper) {
                 System.out.println("Computer wins!");
                 return;
                 } else if (computerNumber == scissor) {
                   System.out.println("User wins!");
                   return;
                 }
                 } else if (userNumber == paper) {
                   if (computerNumber == scissor) {
                     System.out.println("Computer wins!");
     
                return;
                } else if (computerNumber == rock) {
                  System.out.println("User wins!");
     
    return;
     
    }
     
    } else {
     
    System.out.println("User wins!");
     
    return;
     
     
    }
     
     
     
         }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,817
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: rock paper scissors

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    repeat itself 10x
    Use a for loop
    If you don't understand my answer, don't ignore it, ask a question.

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

    Audz (February 18th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: rock paper scissors

    where exactly in this code would i place the for loop so i can get it compiled?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,817
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: rock paper scissors

    Put the for() with its { just before the first statement which you want repeated after the loop ends
    and put the end } after the last statement that should execute before the code loops to the beginning for the next time around.

    Move the return statements outside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Audz (February 18th, 2014)

Similar Threads

  1. ROCK, PAPER, SCISSORS GAME.
    By kofiachie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 21st, 2013, 07:24 AM
  2. Creating a GUI for a rock, paper, scissors game
    By nmg13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 02:09 PM
  3. Rock paper scissors program not displaying winner
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 04:38 AM
  4. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 10:34 PM
  5. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM