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

Thread: Please help with the switch statement for rock paper scissor game using JOptionPane.

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

    Default Please help with the switch statement for rock paper scissor game using JOptionPane.

    Please help me out I am the biginner in java.I I suppose to write a program that plays the popular rock-paper game.The program should randomly generate the random number 0,1 or 2 representing the rock, paper or scissor. The program should randomly ask the user to enter number 0,1 or 0 and display the message indicating whether the user or the computer wins or draws.

    NOTE: A scissor can cut a paper, a rock can crush a scissor and the paper can wrap a rock.


    I am require to use math.random() medhod. To get random number between 0 and 2. I am required to use the following code:
    (int)(Math.random() *)


    Note: Math.radom return a double value between 0 and 1.

    Finally JOptionPane is required for input and out.

    Here is what i have sor far but I am getting few bugs. Please help I would reallly appreciate.



    import javax.swing.JOptionPane; // Need JOptionPane class
    import java.util.Random; // Need Random class
    public class RockPaperScissorGameJOptionPane{
    public static void main(String [] args){


    // Please decleare the variables
    String inputRock;
    String inputScissor;
    String inputNumber;
    String inputPaper;
    int number;
    int paper;
    int scissor;
    int random;
    int randomNumber;
    int rock;

    rock = 0;
    paper = 1;
    scissor = 2;


    // Please prompt the user to enter the number 0, 1, and 2.
    JOptionPane.showInputDialog(" Please ask the user to enter number 0,1 2 or enter 4 to exit ");
    number = Integer.parseInt(inputNumber);

    randomNumber = (int)(Math.random() *3);


    switch(number){
    case 1:
    if(randomNumber == 2){
    JOptionPane.showMessageDialog(null, " The computer wins.");
    }
    else if (randomNumber ==0){
    JOptionPane.showMessageDialog(null, " The game is a draw.");
    }
    else if (randomNumber == 1){
    JOptionPane.showMessageDialog(null,"The user is wins.");
    }
    break;

    case 2:
    if (randomNumber ==0){
    JOptionPane.showMessageDialog(null,"The computer wins.");
    }
    else if(randomNumber ==1){
    JOptionPane.showMessageDialog(null,"The game is draw.");
    }
    else if(randomNumber == 2){
    JOptionPane.showMessageDialog(null,"The user wins.");
    }
    break;

    case 3:
    if (randomNumber == 1){
    JOptionPane.showMessageDialog(null, " The computer wins.");
    }
    else if (randomNumber == 2){
    JOptionPane.showMessageDialog(null, " The game is a draw.");
    }
    else if (randomNumber == 0){
    JOptionPane.showMessageDialog(null, " The user wins.");
    }
    break;

    case4:
    System.exit(0);
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    t I am getting few bugs.
    If you are getting error messages, copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    C:\Users\lyared.METROSTATE\Desktop\RockPaperScisso rGameJOptionPane.java:31: error: variable inputNumber might not have been initialized
    number = Integer.parseInt(inputNumber);
    ^
    C:\Users\lyared.METROSTATE\Desktop\RockPaperScisso rGameJOptionPane.java:73: error: unreachable statement
    case4:
    ^
    2 errors

    Tool completed with exit code 1

    --- Update ---

    Every time I am running it I am getting these errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    variable inputNumber might not have been initialized
    The compiler can not find where inputNumber has been given a value before it is used.
    Where does the code assign it a value?

    I can't tell about the other error because the code is not properly formatted.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    I have never done formating before could please explain to me how to do it and i will post my whole code in a minute thanks alot.

    --- Update ---

    I am currently using textpad

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    Look at some of the code posted on other threads. Statements nested within {} should be indented 3-4 spaces.
    see this:
    Code Conventions for the Java Programming Language: Contents
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    // Edwin Lyaruu
    // Programming Exercise 3.17
    // February 25,2013
    // This program plays the popular rock-paper-scissor game
     
    import javax.swing.JOptionPane;   // Need JOptionPane class
    import java.util.Random;         // Need  Random class
    public class RockPaperScissorGameJOptionPane{
    public static void main(String []  args){
     
     
    // Please decleare the variables
       String inputRock;
       String inputScissor;
       String inputNumber;
       String inputPaper;
       int number;
       int paper;
       int scissor;
       int random;
       int randomNumber;
       int rock;
     
       rock = 0;
       paper = 1;
       scissor = 2;
     
     
    // Please prompt the user to enter the number 0, 1, and 2.
       JOptionPane.showInputDialog(" Please ask the user to enter number 0,1 2 or enter 4 to exit ");
       number = Integer.parseInt(inputNumber);
     
     randomNumber = (int)(Math.random() *3);
     
     
     switch(number){
          case  1:
               if(randomNumber == 2){
               JOptionPane.showMessageDialog(null, " The computer wins.");
               }
               else if (randomNumber ==0){
               JOptionPane.showMessageDialog(null, " The game is a draw.");
               }
               else if (randomNumber == 1){
               JOptionPane.showMessageDialog(null,"The user is wins.");
               }
               break;
     
          case 2:
                if (randomNumber ==0){
                JOptionPane.showMessageDialog(null,"The computer wins.");
                }
                else if(randomNumber ==1){
                JOptionPane.showMessageDialog(null,"The game is draw.");
                }
                else if(randomNumber == 2){
                JOptionPane.showMessageDialog(null,"The user wins.");
                }
                break;
     
          case 3:
                if (randomNumber == 1){
                JOptionPane.showMessageDialog(null, " The computer wins.");
                }
                else if (randomNumber == 2){
                JOptionPane.showMessageDialog(null, " The game is a draw.");
                }
                else if (randomNumber == 0){
                JOptionPane.showMessageDialog(null, " The user wins.");
                }
                break;


    --- Update ---

    // Edwin Lyaruu
    // Programming Exercise 3.17
    // February 25,2013
    // This program plays the popular rock-paper-scissor game
     
    import javax.swing.JOptionPane;   // Need JOptionPane class
    import java.util.Random;         // Need  Random class
    public class RockPaperScissorGameJOptionPane{
    public static void main(String []  args){
     
     
    // Please decleare the variables
       String inputRock;
       String inputScissor;
       String inputNumber;
       String inputPaper;
       int number;
       int paper;
       int scissor;
       int random;
       int randomNumber;
       int rock;
     
       rock = 0;
       paper = 1;
       scissor = 2;
     
     
    // Please prompt the user to enter the number 0, 1, and 2.
       JOptionPane.showInputDialog(" Please ask the user to enter number 0,1 2 or enter 4 to exit ");
       number = Integer.parseInt(inputNumber);
     
     randomNumber = (int)(Math.random() *3);
     
     
     switch(number){
          case  1:
               if(randomNumber == 2){
               JOptionPane.showMessageDialog(null, " The computer wins.");
               }
               else if (randomNumber ==0){
               JOptionPane.showMessageDialog(null, " The game is a draw.");
               }
               else if (randomNumber == 1){
               JOptionPane.showMessageDialog(null,"The user is wins.");
               }
               break;
     
          case 2:
                if (randomNumber ==0){
                JOptionPane.showMessageDialog(null,"The computer wins.");
                }
                else if(randomNumber ==1){
                JOptionPane.showMessageDialog(null,"The game is draw.");
                }
                else if(randomNumber == 2){
                JOptionPane.showMessageDialog(null,"The user wins.");
                }
                break;
     
          case 3:
                if (randomNumber == 1){
                JOptionPane.showMessageDialog(null, " The computer wins.");
                }
                else if (randomNumber == 2){
                JOptionPane.showMessageDialog(null, " The game is a draw.");
                }
                else if (randomNumber == 0){
                JOptionPane.showMessageDialog(null, " The user wins.");
                }
                break;
    Last edited by Norm; February 27th, 2013 at 06:29 PM. Reason: Fixed some of the code tags

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    Take another look at how to spell the code tags. Most of the code tags you used needed fixing.

    Here is how wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    //gram plays the popular rock-paper-scissor game

    import javax.swing.JOptionPane; // Need JOptionPane class
    import java.util.Random; // Need Random class
    public class RockPaperScissorGameJOptionPane{
    public static void main(String [] args){

    randomNumber = (int)(Math.random() *3);

    // Please decleare the variables
    String inputRock;
    String inputScissor;
    String inputNumber;
    String inputPaper;
    int number;
    int paper;
    int scissor;
    int random;
    int randomNumber;
    int rock;



    rock = 0;
    paper = 1;
    scissor = 2;


    // Please prompt the user to enter the number 0, 1, and 2.
    JOptionPane.showInputDialog(" Please ask the user to enter number 0,1 2 or enter 4 to exit ");
    number = Integer.parseInt(inputNumber);


    switch(number){
    case 1:
    if(randomNumber == 2){
    JOptionPane.showMessageDialog(null, " The computer wins.");
    }
    else if (randomNumber ==0){
    JOptionPane.showMessageDialog(null, " The game is a draw.");
    }
    else if (randomNumber == 1){
    JOptionPane.showMessageDialog(null,"The user is wins.");
    }
    break;

    case 2:
    if (randomNumber ==0){
    JOptionPane.showMessageDialog(null,"The computer wins.");
    }
    else if(randomNumber ==1){
    JOptionPane.showMessageDialog(null,"The game is draw.");
    }
    else if(randomNumber == 2){
    JOptionPane.showMessageDialog(null,"The user wins.");
    }
    break;

    case 3:
    if (randomNumber == 1){
    JOptionPane.showMessageDialog(null, " The computer wins.");
    }
    else if (randomNumber == 2){
    JOptionPane.showMessageDialog(null, " The game is a draw.");
    }
    else if (randomNumber == 0){
    JOptionPane.showMessageDialog(null, " The user wins.");
    }
    break;

    case4:
    System.exit(0);
    }
    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please help with the switch statement for rock paper scissor game using JOptionPane.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM
  2. I REALLY NEED HELP IN SOLVING THIS ROCK PAPER SCISSOR
    By John93 in forum Java Theory & Questions
    Replies: 12
    Last Post: April 8th, 2012, 08:55 PM
  3. rock paper scissor. whats wrong here ??
    By bjorn10 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 10th, 2011, 09:13 AM
  4. rock paper si
    By robingeldolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 4th, 2011, 06:41 AM
  5. MyThesis: rock,paper,scissor..on mobile
    By Cross`17 in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: April 22nd, 2010, 08:33 AM