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

Thread: I can't get my last method to work! What am I doing wrong?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy I can't get my last method to work! What am I doing wrong?

    import java.util.Random;
    import java.util.Scanner;
     
    public class RockPaperScissors
    {
    	public static void main (String[] args)
    	{
     
    	 	Random rand = new Random();	
    		System.out.println ("How about a friendly game "+
    				"Rock, Paper, Scissors?");
    		System.out.println ("Choose 1 for rock, 2 for paper" +
    			" or 3 for scissors"+
    			" then hit enter.");
     
    		userPick();
    		computerPick();	
    		winner();
    	}
    	public static void userPick()
    	{
    		int userPick;		
    		Scanner keyboard = new Scanner(System.in);
    		userPick = keyboard.nextInt();
     
    			switch (userPick)
    			{
    				case 1: 
    					System.out.println("You chose ROCK");
    					break;
    				case 2:
    					System.out.println("You chose PAPER");
    					break;
    				case 3:
    					System.out.println("You chose SCISSORS");
    					break;
    				default:
    					System.out.println("Invalid choice");
    					break;
    			}
     
    	}		
    	public static void computerPick()
    	{
    		int computerWeapon;
    		Random weapon = new Random();
    		computerWeapon = (weapon.nextInt(3)+ 1);
     
    			switch (computerWeapon)
    			{
    				case 1:
    					System.out.println("Computer chose ROCK");
    					break;
    				case 2:
    					System.out.println("Computer chose PAPER");
    					break;
    				case 3:
    					System.out.println("Computer chose SCISSORS");
    					break;
    			}
     
    	}
    	public static void winner()
    	{
    		int computerWeapon;
    		Random weapon = new Random();
    		computerWeapon = (weapon.nextInt(3)+ 1);
    		int userPick;		
    		Scanner keyboard = new Scanner(System.in);
    		userPick = keyboard.nextInt();
     
    			if (computerWeapon == 1)
    			{
    				if (userPick==2)
    					System.out.println ("Paper covers rock. "+
    						"You WIN!");
    				else if (userPick==3)
    					System.out.println ("Rock crushes scissors. " +
    						"You LOSE!");
    				else if (userPick==1)
    					System.out.println ("You both chose rock. "+
    						"It's a tie game!");
    			}
    			else if (computerWeapon == 2)
    			{
    				if (userPick == 1)
    					System.out.println ("Paper covers rock. "+
    						"You LOSE!");
    				if (userPick == 3)
    					System.out.println ("Scissors cut paper. "+
    						"You WIN!");
    				else if (userPick ==2)
    					System.out.println ("It's a tie game!");
    			}
    			else if (computerWeapon == 3)
    			{
    				if (userPick == 1)
    				System.out.println ("Rock crushes scissors. " +
    					"You WIN!");
    				if (userPick == 2)
    				System.out.println ("Scissors cut paper. "+
    					"You LOSE!");
    				else if (userPick == 3)
    					System.out.println ("It's a tie game!");
    			}
     
    		}
     
     
    }

    The game runs, but only up until the winner part, that does not run.
    Last edited by deejeridoozy; September 18th, 2013 at 07:56 PM. Reason: Fixed some problems


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,855
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: I can't get my last method to work! What am I doing wrong?

    Please post your code in code tags. You can learn how in the Announcements topic at the topic of the WWWMC forum.

    Where does the method computerPick() end? Hint: Methods can't be defined inside other methods.

    Once you determine that, you may discover you have undefined variables, because they were defined as local to computerPick(). Let us know if you need help figuring that one out.

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

    Default Can't seem to get my last method "winner" to work.

    import java.util.Random;
    import java.util.Scanner;
     
    public class wtf
    {
    	public static void main (String[] args)
    	{
     
    	 	Random rand = new Random();	
    		System.out.println ("How about a friendly game "+
    				"Rock, Paper, Scissors?");
    		System.out.println ("Choose 1 for rock, 2 for paper" +
    			" or 3 for scissors"+
    			" then hit enter.");
     
    		userPick();
    		computerPick();	
    		winner();
    	}
    	public static void userPick()
    	{
    		int userPick;		
    		Scanner keyboard = new Scanner(System.in);
    		userPick = keyboard.nextInt();
     
    			switch (userPick)
    			{
    				case 1: 
    					System.out.println("You chose ROCK");
    					break;
    				case 2:
    					System.out.println("You chose PAPER");
    					break;
    				case 3:
    					System.out.println("You chose SCISSORS");
    					break;
    				default:
    					System.out.println("Invalid choice");
    					break;
    			}
     
    	}		
    	public static void computerPick()
    	{
    		int computerWeapon;
    		Random weapon = new Random();
    		computerWeapon = (weapon.nextInt(3)+ 1);
     
    			switch (computerWeapon)
    			{
    				case 1:
    					System.out.println("Computer chose ROCK");
    					break;
    				case 2:
    					System.out.println("Computer chose PAPER");
    					break;
    				case 3:
    					System.out.println("Computer chose SCISSORS");
    					break;
    			}
    	public static void winner()
    	{
     
    			if (computerWeapon == 1)
    			{
    				if (userPick==2)
    					System.out.println ("Paper covers rock. "+
    						"You WIN!");
    				else if (userPick==3)
    					System.out.println ("Rock crushes scissors. " +
    						"You LOSE!");
    				else if (userPick==1)
    					System.out.println ("You both chose rock. "+
    						"It's a tie game!");
    			}
    			else if (computerWeapon == 2)
    			{
    				if (userPick == 1)
    					System.out.println ("Paper covers rock. "+
    						"You LOSE!");
    				if (userPick == 3)
    					System.out.println ("Scissors cut paper. "+
    						"You WIN!");
    				else if (userPick ==2)
    					System.out.println ("It's a tie game!");
    			}
    			else if (computerWeapon == 3)
    			{
    				if (userPick == 1)
    				System.out.println ("Rock crushes scissors. " +
    					"You WIN!");
    				if (userPick == 2)
    				System.out.println ("Scissors cut paper. "+
    					"You LOSE!");
    				else if (userPick == 3)
    					System.out.println ("It's a tie game!");
    			}
     
    	}
     
     
    }

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Can't seem to get my last method "winner" to work.

    Post the errors you are getting and what behavior the application is doing and what it's suppose to be doing.

    Take a look at the scope of variables.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can't get my last method to work! What am I doing wrong?

    OMG, thank you for pointing that out! I fixed it all and it compiles fine, but the winner portion of the game doesn't seem to execute :/

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

    Default Re: I can't get my last method to work! What am I doing wrong?

    Duplicate questions
    Threads merged

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: I can't get my last method to work! What am I doing wrong?

    post the code you have now.

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 2013, 01:32 AM
  2. Whats wrong with my code? I tried several things but it still won't work!
    By onoemre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 12:55 PM
  3. Replies: 1
    Last Post: January 23rd, 2013, 06:29 AM
  4. Can't Get Timer to Work, What Am I doing Wrong?!?!
    By Liikeaturtle in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2012, 08:43 PM
  5. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 03:16 AM

Tags for this Thread