Results 1 to 4 of 4
  1. #1
    StevenF is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Rock, paper, scissors

    Hey there, I'm new to Java and made a simple rock, paper, scissors program using Random() and if-else. Just curious as to whether or not there are different ways of doing it (I'm sure there are, but this is what I came up with).

    Java Code:
    // Rock, paper, scissors program that will ask for input
    // then compare to random choice by computer
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class RockPaperScissors {
    
    	public static void main(String[] args) {
    
    		// Rock Paper and Scissors as Static Values
    		int ROCK = 0;
    		int SCISSORS = 1;
    		int PAPER = 2;
    		
    		// Variable intChoice for comparing to randomInt
    		int intChoice = 4;
    		
    		// New Scanner
    		Scanner key = new Scanner(System.in);
    		
    		System.out.print("Please enter rock, paper, or scissors: ");
    		String choice = new String(key.nextLine());
    		
    		if (choice.compareToIgnoreCase("rock") == 0)
    			intChoice = ROCK;
    		else if (choice.compareToIgnoreCase("paper") == 0)
    			intChoice = PAPER;
    		else if (choice.compareToIgnoreCase("scissors") == 0)
    			intChoice = SCISSORS;
    		else 
    			System.out.println("You must enter either rock, paper, or scissors.");
    			
    
    		// New random generator
    		Random randomGenerator = new Random();
    		int randomInt = randomGenerator.nextInt(3);
    		
    		if (intChoice == randomInt)
    			System.out.println("Tie!");
    		else if (intChoice == 0 && randomInt == 1)
    			System.out.println("Rock beats scissors! You win!");
    		else if (intChoice == 0 && randomInt == 2)
    			System.out.println("Paper covers rock... You lose.");
    		else if (intChoice == 1 && randomInt == 0)
    				System.out.println("Rock beats scissors... you lose.");
    		else if (intChoice == 1 && randomInt == 2)
    				System.out.println("Scissors cuts paper! You win!");
    		else if (intChoice == 2 && randomInt == 0)
    			System.out.println("Paper covers rock! You win!");
    		else if (intChoice == 2 && randomInt == 1)
    			System.out.println("Scissors cuts paper... You lose.");
    		else 
    			System.out.println();
    		
    		
    	}
    
    }
    EDIT: Small fix when entering something other than rock, paper, or scissors.
    Last edited by StevenF; 01-30-2011 at 09:58 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    It's a good idea to declare your constants as final, and to have them outside the body of the main method.

  4. #4
    StevenF is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    It's a good idea to declare your constants as final, and to have them outside the body of the main method.
    Thanks, forgot about that.

Similar Threads

  1. Paper rock Scissors Game inheritance problem
    By kaanax in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 02:44 PM
  2. Stone, Paper, Scissors game help
    By Harris68 in forum Networking
    Replies: 1
    Last Post: 12-19-2009, 04:33 PM
  3. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 06:23 AM
  4. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 03:35 AM
  5. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •