DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    2

    Stumped on program project

    I am trying to writing a program that does the Rock Paper Scissor game, against the computer
    using keyboard input, and the buffered reader. also keeping track of wins, loses, ties

    I managed to get the input through the buffered reader, the problem is I am stumped on what the next step is. any help would be appreciated.


    also when compiling I get these two errors:
    rps2.java: 44: illegal start of expression else if(player === PAPER)
    ^
    rps2.java:59: ')' expected }

    Code:
    import java.io.*;
    import java.util.*;
    
    public class rps2
    {		
    		final int ROCK = 0, PAPER = 1, SCISSORS = 2;
    		int computer, player;
    		int COMP = 0, PLAYER = 0;
    		int Cwins, Closses, Pwins, Plosses,ties, winner = 0;
    		
    	public static void main (String [] args)throws IOException
    	{
    		String string_1;
    		
    		System.out.println("Enter *0*-Rock | *1* Paper | *2*Scissors : ");
    		BufferedReader stdin =
    			new BufferedReader( new InputStreamReader(System.in));		
    		
    		string_1 = stdin.readLine();
            int player =  Integer.parseInt(string_1);     
    		getUserInput();
    	}	
    	public void getUserInput()
    	{	
    		computer = getRandom();
    	
    		switch(computer)
    		{
    		case ROCK:
    			if (player === SCISSORS )
    			{
    				System.out.println("Computer Wins");
    				break;
    			}
    			else if (player == PAPER)
    			{
    				System.out.println("Player WINS");
    				break;
    			}
    			else
    			{
    				System.out.println("Tie");
    				break;		
    			}
    		}		
    	}
    	
    	public int getRandom()
    	{
    		return  (int)(Math.random()*3)+1;
    	}	
    }

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    When checking equality, you're using 3 '=' rather than 2 i.e. '===' rather than '=='.

    The methods getUserInput() and getRandom() should be declared static as a static method (such as main) cannot directly call a non-static one.

    All of your instance variables must be declared static because once you've made getUserInput() static, they cannot be referenced from a static method.

    Also it's a convention in Java to declare classes with a capital letter i.e. 'RockPaperScissor' rather than 'rockpaperscissor' or 'rps'.

    It's also not a good idea to name variables such as 'string_1' - the name isn't very helpful.

    Unless variables are to be used outside a class (such as 'ROCK') they should be declared private.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    2
    thanks for advice and tips ArchAngel much appreciated

    i am still learning the conventions of java,
    the static is what i am having a problem with though.

    so the variables should be
    int static computer

    correct?
    Peace

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    No, you have the wrong syntax:
    Code:
      private static int computer;
    'static' BEFORe 'int'.
    ArchAngel.
    O:-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links