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

Thread: I cannot find the problem with this code.

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

    Default I cannot find the problem with this code.

    // ************************************************** **************
    // Rock.java
    //
    // Play Rock, Paper, Scissors with the user
    // ************************************************** **************
    import java.util.Scanner;
    import java.util.Random;

    public class Rock
    {
    public static void main(String[] args)
    {
    String personPlay; //User's play -- "R", "P", or "S"
    String computerPlay = ""; //Computer's play -- "R", "P", or "S"
    int computerInt; //Randomly generated number used to determine
    //computer's play

    Scanner scan = new Scanner(System.in);
    Random generator = new Random();

    //Generate computer's play (0,1,2)
    computerInt = generator.nextInt(3);


    //Translate computer's randomly generated play to string using if
    //statements or a switch statement
    switch (computerInt)
    {
    case 0: computerPlay = "R"; break;
    case 1: computerPlay = "P"; break;
    case 2: computerPlay = "S"; break;

    }



    //Get player's play from input-- note that this is stored as a string
    System.out.println ("Choose: R(Rock), P(Paper), S(Scissor): ");
    personPlay = scan.nextLine();


    //Make player's play uppercase for ease of comparison
    personPlay = personPlay.toUpperCase();


    //Print computer's play
    System.out.println ("Player1: " + personPlay);


    //See who won. Use nested ifs
    if (personPlay.equals(computerPlay))
    {
    System.out.println("It's a tie!");
    }

    else if (personPlay.equals("R"))
    {
    if (computerPlay.equals("S"))
    {
    System.out.println("Rock crushes scissors. You win!!");
    }
    else
    {
    System.out.println ("Paper covers rock. You Lose!");
    }
    }

    else if (personPlay.equals("P"))
    {
    if (computerPlay.equals("R"))
    {
    System.out.println ("Paper cover rock. You Win!");
    }
    else
    {
    System.out.println ("Scissors cut paper. You Lose!");
    }
    }

    // The error is with these statements below. They are executed even if the the conditions are not met.

    else if (personPlay.equals("S"));
    {
    if (computerPlay.equals("R"))
    {
    System.out.println ("Rock crushes scissor. You Lose!");
    }
    else
    {
    System.out.println ("Scissors cut paper. You Win!");
    }
    }



    } // end of method
    }// end of class


  2. #2
    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 cannot find the problem with this code.

    Welcome LiveH

    Please use code tags when posting code. See the Announcements page for help
    What seems to be the trouble with the code?
    What should it do? What does it do? Where do things seem to go unexpected (line number?)?

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: I cannot find the problem with this code.

    // The error is with these statements below. They are executed even if the the conditions are not met.
    How do you know the conditions are not met? Print out the values of the interesting conditions before you start the if blocks. This will help with debugging.

    I agree with jps - without code formatting it's too hard to follow the logic. (And programmers are unapologetic to the point of being proud of their laziness! Why try and figure out what depends on what when the indentation should make it obvious...?). Have a go at editing/reposting and ask if the instructions are incomprehensible.

Similar Threads

  1. [SOLVED] Can't find solution for error in my code..
    By adi2609 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 26th, 2013, 11:10 AM
  2. [SOLVED] Can't find mistake in my code
    By Daler in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 19th, 2012, 12:19 AM
  3. Can't Find what is wrong with my Code
    By Raptorman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 04:12 PM
  4. Can't Find Problem
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 15th, 2010, 09:42 AM
  5. I can't find out what is wrong with my code. Please Help!
    By hallor618 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2010, 02:44 PM