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

Thread: immediate !! Rock Paper Scissiors Game

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default immediate !! Rock Paper Scissiors Game

    it resets the score each time. I want it to end when the score is five.

    import javax.swing.JOptionPane;
     
     
    public class Chapter5RockPaperScissors {
        public static void main(String[] args) {
            String computer, person;
     
            JOptionPane.showMessageDialog(null, "The first player who reaches 5 points wins");
            for(int i = 0; i<5; i++) {
     
                computer = ComputerChoice();
                person = UserChoice();
     
     
                JOptionPane.showMessageDialog(null, "The computer's choice is " + computer + ".");
     
                Winner(computer, person);
     
     
            }
        }
     
        public static String ComputerChoice() {
            int computerChoice;
     
            String computerChoiceString = "";
     
            computerChoice = (int) (Math.random() * 3);
     
            switch (computerChoice) {
     
                case 0: {
                    computerChoiceString = "Rock";
                    break;
                }
     
                case 1: {
                    computerChoiceString = "Paper";
                    break;
                }
     
                case 2: {
                    computerChoiceString = "Scissiors";
                    break;
                }
     
            }
     
            return computerChoiceString;
        }
     
        public static String UserChoice() {
            String userChoice;
     
            userChoice = JOptionPane.showInputDialog("\nRock\nPaper\nScissiors");
            return userChoice;
     
        }
     
        public static void Winner(String computer, String person) {
            int scorecomputer = 0;
            int scoreperson = 0;
            int scoreperson1;
            int scorecomputer1;
     
     
     
     
            if (computer.equals("Rock")) {
                switch (person) {
                    case "Rock" -> JOptionPane.showMessageDialog(null, "It is tie");
                    case "Paper" -> {
                        JOptionPane.showMessageDialog(null, "Paper cover rock. Person is win");
     
                        scoreperson1 = scoreperson;
                        scoreperson = scoreperson1 + 1;
     
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
                    }
                    case "Scissiors" -> {
                        JOptionPane.showMessageDialog(null, "Rock break Scissiors. Computer is win");
                        scorecomputer1 = scorecomputer;
                        scorecomputer = scorecomputer1 + 1;
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
     
                    }
                    default -> JOptionPane.showMessageDialog(null, "This is wrong. Please choose rock,paper,scissiors.");
                }
            } else if (computer.equals("Paper")) {
                switch (person) {
                    case "Rock" -> {
                        JOptionPane.showMessageDialog(null, "Paper cover rock. Computer is win");
                        scorecomputer1 = scorecomputer;
                        scorecomputer = scorecomputer1 + 1;
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
     
                    }
                    case "Paper" -> JOptionPane.showMessageDialog(null, "It is tie");
                    case "Scissiors" -> {
                        JOptionPane.showMessageDialog(null, "Scissiors cut paper.Person is win");
     
                        scoreperson1 = scoreperson;
                        scoreperson = scoreperson1 + 1;
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
                    }
                    default -> JOptionPane.showMessageDialog(null, "This is wrong. Please choose rock,paper,scissiors.");
                }
            } else if (computer.equals("Scissiors")) {
                switch (person) {
                    case "Rock" -> {
                        JOptionPane.showMessageDialog(null, "Rock break Scissiors. Person is win");
     
                        scoreperson1 = scoreperson;
                        scoreperson = scoreperson1 + 1;
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
                    }
                    case "Paper" -> {
                        JOptionPane.showMessageDialog(null, "Scissiors cut paper. Computer is win");
                        scorecomputer1 = scorecomputer;
                        scorecomputer = scorecomputer1 + 1;
                        JOptionPane.showMessageDialog(null, "Computer Score : "+scorecomputer+"\nPerson Score : "+scoreperson);
     
     
                    }
                    case "Scissiors" -> JOptionPane.showMessageDialog(null, "It is tie");
                    default -> JOptionPane.showMessageDialog(null, "This is wrong. Please choose rock,paper,scissiors.");
                }
     
     
            }
     
        }
     
    }

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

    Default Re: immediate !! Rock Paper Scissiors Game

    How are you trying to debug the code to see what it is doing? Using JOptionPane makes it hard for you to copy the console window's contents and paste it here to show what you are talking about.
    Can you add print statements that print out the values of the variables as they are changed and used to show you what the computer sees when the program is executed? When you have done that you can copy the print out and paste it here with some comments where the program is not doing what you expected.

    it resets the score each time.
    What variable are you talking about? Where is the code that resets its value?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: immediate !! Rock Paper Scissiors Game

    Quote Originally Posted by Norm View Post
    How are you trying to debug the code to see what it is doing? Using JOptionPane makes it hard for you to copy the console window's contents and paste it here to show what you are talking about.
    Can you add print statements that print out the values of the variables as they are changed and used to show you what the computer sees when the program is executed? When you have done that you can copy the print out and paste it here with some comments where the program is not doing what you expected.


    What variable are you talking about? Where is the code that resets its value?
    scorecomputer and scoreperson .The game must be finished when the score is five by the computer or the score person, but it always remains at 1. For example, when I say rock, the computer says paper and goes to the computer. Then the game continues like this five times. Normally, as you win, the score should increase like 1 2 3 4 5, but it doesn't.If you have ide, you can view the code by copying and running it. And you would be very helpful to me.

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

    Default Re: immediate !! Rock Paper Scissiors Game

    For variables to hold values between method calls, they must be declared at the class level. I do not see any class level variables in the code.
    variables that are locally defined inside of a method have their values set each time the method is called.

    The for loop will iterate 5 times no matter what the score. It you want the loop to iterate until some other condition is obtained, then change the loop to another type like a while loop that uses the desired conditions to keep running or to end.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: immediate !! Rock Paper Scissiors Game

    Quote Originally Posted by Norm View Post
    For variables to hold values between method calls, they must be declared at the class level. I do not see any class level variables in the code.
    variables that are locally defined inside of a method have their values set each time the method is called.

    The for loop will iterate 5 times no matter what the score. It you want the loop to iterate until some other condition is obtained, then change the loop to another type like a while loop that uses the desired conditions to keep running or to end.
    Thanks for answering. I am Turkish and my English is a bit poor, if you correct the parts you say and repeat the code, I will be very happy.
    It's okay if you don't want to mess around.

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

    Default Re: immediate !! Rock Paper Scissiors Game

    Try moving the declarations for the variables that hold the score out of the method and declare them at the class level. Then their values will be preserved between calls to methods.

    Here is an example of how to declare a variable at the class level:
    class Testing {
       int aClassLevelVar;   // declare a class level variable
     
      public void someMethod() {
         int aLocalVar = 1;           // declare local variable
         aClassLevelVar = 12; // assign a value to a class level variable
        ...
      }
      public void anotherMethod() {
         int aLocalVar = 2;   // declare a new local variable, nothing to do with other by same name
         ...
         int anotherLocalVar = aClassLevelVar + 1;  // use the class level variable's value
       }
    }

    Sorry, I do not write code for students. You will learn more if you do it yourself.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Rock paper scissors game need help
    By shorba14 in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 17th, 2020, 05:53 AM
  2. Help with paper rock scissors game
    By sjud9227 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 24th, 2014, 07:46 PM
  3. ROCK, PAPER, SCISSORS GAME.
    By kofiachie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 21st, 2013, 07:24 AM
  4. Creating a GUI for a rock, paper, scissors game
    By nmg13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 02:09 PM