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: Need help with a basic 21 Sticks game code!

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a basic 21 Sticks game code!

    Hey everyone, I was wondering what is wrong with my code:

    import java.util.Scanner;
    public class PickUpSticks 
    {
    	public static void main(String[] args) 	
    	{	
    		int numberofsticks = 21;
    		System.out.println("Would you like to go first? (Y/N?)");
    		Scanner user_input = new Scanner(System.in);
    		String goFirst = user_input.nextLine();
    		Scanner take = new Scanner(System.in);
    		int numbertotake = 0;
     
    		while (numberofsticks > 0)
    		{
    			if (goFirst.equals("y") || goFirst.equals("Y"));
    			{
    				System.out.println("There are " + numberofsticks + " sticks.");
    				System.out.println("How many sticks will you take? 1 or 2?");
    				numbertotake = take.nextInt();
    				numberofsticks = numberofsticks - numbertotake;				
     
    					if (numbertotake > 2)
    					{
    						numbertotake = 2;
    					}
    						else if (numbertotake < 1)
    						{
    							numbertotake = 1;
    						}	
     
    				numberofsticks = numberofsticks - numbertotake;		
     
    				if (numberofsticks<=0)
    				{
    					System.out.println("You lose!");
    				}
     
    					else
    					{
    						if ((numberofsticks - 2) % 3 == 0 || numberofsticks - 2 == 0)
    						{
    							numbertotake = 1;
    						}
    							else
    							{
    								numbertotake = 2;
    							}
     
    						System.out.println("Computer takes " + numbertotake + " sticks.");
    						numberofsticks = numberofsticks - numbertotake;
     
    						if (numberofsticks <= 0)
    						{
    							System.out.println("You win!");
    						}							
    					}
    			}			
     
    			else			
    			{
    				if ((numberofsticks - 2) % 3 == 0 || numberofsticks - 2 == 0)
    				{
    					numbertotake = 1;
    				}
    					else
    					{
    						numbertotake = 2;
    					}
     
    				System.out.println("Computer takes " + numbertotake + " sticks.");
    				numberofsticks = numberofsticks - numbertotake;
     
    				if (numberofsticks <= 0)
    				{
    					System.out.println("You win!");
    				}
    					else
    					{
    						System.out.println("There are " + numberofsticks + " sticks.");
    						System.out.println("How many sticks will you take? 1 or 2?");
    						numbertotake = take.nextInt();
     
    						if (numbertotake > 2)
    						{
    							numbertotake = 2;
    						}
    							else if (numbertotake < 1)
    							{
    								numbertotake = 1;
    							}	
     
    						numberofsticks = numberofsticks - numbertotake;		
     
    						if (numberofsticks <= 0)
    						{
    							System.out.println("You lose!");
    						}
     
    					}
    			}				
    		}
    	}
    }

    It looks fine to me, but eclipse says that my "else" needs to be removed. However, if I do, the program goes weird. Help please!
    Last edited by Rasrkiaki; June 24th, 2013 at 02:11 PM. Reason: Edited for better java format.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Need help with a basic 21 Sticks game code!

    Every time you see such an error message, think "problem with braces" as in {} or an empty statement, like a ; after a for(i;c;u)

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a basic 21 Sticks game code!

    I see. I've looked at my brackets though, and they all seem fine. Same with the semicolons and all my syntax seems correct.

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Need help with a basic 21 Sticks game code!

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on token "else", delete this token

    at PickUpSticks.main(PickUpSticks.java:58)

    i tried it and removed the else and works fine after that so remove the else just the word else at line 58 like the error message tells you to...if it doesnt work after that then its your IDE or something cause it works fine for me

Similar Threads

  1. Method error for checking and updating guess in a basic hangman game
    By lotusmind in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 13th, 2013, 10:03 PM
  2. Basic Number guessing game.
    By Ddng940 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2012, 07:01 PM
  3. Basic Guessing Game
    By djl1990 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 05:14 PM
  4. Basic 2D Game Help Needed
    By Kez323 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 13th, 2012, 02:37 PM
  5. Basic Java maze game
    By sciontc1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 09:18 AM