Results 1 to 18 of 18
  1. #1
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Exclamation Java Question Need Help Restarting the Values of Variables from Random Numbers So Out

    Problem fixed, thank you all for the suggestions.
    Last edited by JavaStudent1990; 07-24-2010 at 10:04 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    http://aswiftlytiltingplanet.net/driver.java
    http://aswiftlytiltingplanet.net/PaperRockScissors.java

    I fixed the main problem (the object is now created for each round), but now I just need to fix the output of the wins and losses. I would like them to increase gradually, not what it's doing (sporadically changing).

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    You're doing all the work (i.e. play all the games) in your constructor but you're not printing any results in that constructor. The while loop in your main method simply prints out the same results over and over again.

    kind regards,

    Jos

    ps. better post your code (or a relevant fragment thereof) in this forum. Not everybody is willing to download and open unknown files)

  5. #5
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I fixed the problem.
    Last edited by JavaStudent1990; 07-24-2010 at 10:04 PM. Reason: Code tags added

  6. #6
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I fixed the problem.
    Last edited by JavaStudent1990; 07-24-2010 at 10:03 PM. Reason: Code tags added

  7. #7
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    Also, I'm studying for a test, and the driver has to be the one that prints the messages--not the constructor.

    "For each hand your Driver will print a message saying what the computer picked, what the user picked and who won the hand. It will then print the score: Human: 1 Computer 0
    Human: 1 Computer 1"

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Reread my previous reply (reply #4) and manually check how your main( ... ) method gets those numbers again and again (hint: nothing happens that could change them). You would've gotten similar constant values in C++.

    kind regards,

    Jos

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    java student: I hope you don't mind that I edited your posts and added code tags to help make your code readable. To learn how to do this yourself, please click on the related link in my signature.

    Much luck!

  10. #10
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I fixed the problem.

    Thank you all for the suggestions.
    Last edited by JavaStudent1990; 07-24-2010 at 10:04 PM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaStudent1990 View Post
    The thing is, they are changing.
    If this is the body of your loop in the main( ... ) method:

    Java Code:
               PaperRockScissors game;
                game = new PaperRockScissors(inGames);
                System.out.println("You " + game.getResult( ) + ".\nThe user wielded " + game.getUserChoice( ) + " and the computer wielded " + game.getComputerChoice( ) + ".");
                System.out.println("Score\nHuman: " + game.getWins( ) + " Computer: " + game.getLosses( ));
    ... no numbers are changing because nothing happens in those few methods you are calling from that loop body. Check it.

    kind regards,

    Jos

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    To the original poster, please do not cross-post without notifying us that you are doing this.

    The problem is that you're asking unpaid volunteers to help you here, not paid consultants, and most of us mind if we take time away from work/lives/play to try to help someone only to find that we've provided a solution that was already provided in a cross-post hours ago. The proper thing to do is that if you feel you must cross-post, at least provide links in each cross-post to the other, so that folks don't waste time answering something already answered and so that folks can review all discussions that have already occurred.

    Thank you for your cooperation.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The OP has deleted his questions and code which is also not a good thing, as this forum is not about just answering his question but also about folks in the future with similar problems looking at this thread and using the solutions suggested to help solve their problems.

    For what it's worth, here is some of his original code:

    Java Code:
    import java.util.*;
    
    /**
     * Write a description of class PaperRockScissors here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class PaperRockScissors
    {
        // instance variables - replace the example below with your own
        private int userRandomNumber;
        private int computerRandomNumber;
        private int numberOfGames;
        private int restart = 1;
        private int winCounter = 0;
        private int lossesCounter = 0;
        
        String result;
        String userChoice;
        String computerChoice;
        
        Random randomChoice = new Random();
        
        public PaperRockScissors( )
        {
            userRandomNumber = -1;
            computerRandomNumber = -1;
            numberOfGames = 0;
            restart = 0;
            winCounter = 0;
            lossesCounter = 0;
    
        }
        
        public PaperRockScissors(int inGames)
        {
            numberOfGames = inGames;
            
            while (numberOfGames > 0)
            {
                userRandomNumber = randomChoice.nextInt(3);
                computerRandomNumber = randomChoice.nextInt(3);
            
                if(userRandomNumber == 0 && computerRandomNumber == 0)
                {
                    userChoice = "paper";
                    computerChoice = "paper";
                    result = "have a draw";
                }
                else if(userRandomNumber == 0 & computerRandomNumber == 1)
                {
                    userChoice = "paper";
                    computerChoice = "rock";
                    result = "won";
                    winCounter++;
                }
                else if(userRandomNumber == 0 && computerRandomNumber == 2)
                {
                    userChoice = "paper";
                    computerChoice = "scissors";
                    result = "lost";
                    lossesCounter++;
                }
                else if(userRandomNumber == 1 && computerRandomNumber == 0)
                {
                    userChoice = "rock";
                    computerChoice = "paper";
                    result = "lost";
                    lossesCounter++;
                }
                else if(userRandomNumber == 1 & computerRandomNumber == 1)
                {
                    userChoice = "rock";
                    computerChoice = "rock";
                    result = "have a draw";
                }
                else if(userRandomNumber == 1 && computerRandomNumber == 2)
                {
                    userChoice = "rock";
                    computerChoice = "scissors";
                    result = "won";
                    winCounter++;
                }
                else if(userRandomNumber == 2 && computerRandomNumber == 0)
                {
                    userChoice = "scissors";
                    computerChoice = "paper";
                    result = "won";
                    winCounter++;
                }
                else if(userRandomNumber == 2 & computerRandomNumber == 1)
                {
                    userChoice = "scissors";
                    computerChoice = "rock";
                    result = "lost";
                    lossesCounter++;
                }
                else if(userRandomNumber == 2 && computerRandomNumber == 2)
                {
                    userChoice = "scissors";
                    computerChoice = "scissors";
                    result = "have a draw";
                }
                
                numberOfGames--;
            }
        }
        
        public String getUserChoice( )
        {
            return userChoice;
        }
        
        public String getComputerChoice( )
        {
            return computerChoice;
        }
        
        public String getResult( )
        {
            return result;
        }
        
        public int getWins( )
        {
            return winCounter;
        }
        
        public int getLosses( )
        {
            return lossesCounter;
        }
    
    
    }

  15. #15
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I would prefer that my code not be re-posted, as it is not something my school would want online out of respect for the class. Future students might just copy and paste the code rather than figure it out and ask their own questions in order to learn. Thus my original hesitation to post it in text. That would be why I deleted the code. I needed advice, and you all asked for what I was working on, so I complied. I just would wish that the code not be re-posted.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaStudent1990 View Post
    I just would wish that the code not be re-posted.
    This forum also wants to be of value to anyone seeking help; you ask a question here so you should post the relevant code to inform your readers; those readers can be future readers so removing your code afterwards is a bit of a nono.

    kind regards,

    Jos

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JavaStudent1990 View Post
    I would prefer that my code not be re-posted, as it is not something my school would want online out of respect for the class. Future students might just copy and paste the code rather than figure it out and ask their own questions in order to learn. Thus my original hesitation to post it in text. That would be why I deleted the code. I needed advice, and you all asked for what I was working on, so I complied. I just would wish that the code not be re-posted.
    We like to retain code here for several reasons. One as Jos points out and as I mentioned, for the benefit of future coders with similar questions, and two, for the benefit of the schools as your instructors will need to know that you have asked this question here and have received whatever help you have received. As a past instructor, believe me, I know that they will want to know this. So for these reasons and others, the code should stay.

  18. #18
    Join Date
    Jul 2010
    Posts
    12
    Rep Power
    0

    Default

    I'm a girl, so it would be her code. =P

    In that case, I might as well post the working code once I don't feel like I'd be violating an honor code, giving out my code. I got everything working properly.

Similar Threads

  1. generate pseudo random numbers in java
    By csr81 in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2010, 08:08 AM
  2. Replies: 5
    Last Post: 12-21-2009, 02:17 PM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. RTP question about random numbers
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:58 AM
  5. random numbers without using java funtions
    By carlos123 in forum New To Java
    Replies: 8
    Last Post: 11-16-2007, 11:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •