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

Thread: computeWinnings()

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default computeWinnings()

    My while loop sometimes opens my getPayMult(), but not often so I cannot get the value to compute my winnings with computeWinnings() and report this number back to Display(). Plus, I have to enter my bet 3 times before I can get the String pull() output Here is my while loop:
    while (getBet()=!0)
    {
       strBet = getBet();
       String pullOne = pull();
       String pullTwo = pull();
       String pullThree = pull();
       int strMult = getPayMultiplier(pullOne,pullTwo,pullThree);
       int strComputeWinnings = getBet()*mult;
       int winnings = computeWinnings()
       display(pullOne,pullTwo,pullThree,ComputeWinnings);
    }
    System.out.println("Thanks for playing.");
    Now look at my output window.
    [code = java]
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    BAR BAR BAR
    You won $3500
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    BAR BAR CHERRIES
    Sorry, you lose.
    Enter bet:
    To stop playing, enter 0:
    0
    Thanks for playing.
    [/code]
    If CHERRIES appears in the 1st position, I should get a payout of 3 times the bet, but my output says, "Sorry, you lose." If I get BAR BAR BAR or 7 7 7 I do get the correct payout (see output above). Could someone please point me in the right direction. Right now I seem to be going around in circles.
    Last edited by willie lee; October 6th, 2012 at 08:02 AM.

  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: computeWinnings()

    You will have to post the related code to get much help with it. We have no way to know what those methods do. All we can see is you call some methods from a loop and don't like what they do.
    Also please try to reserve [code=java] tag for code and something like [quote] and [/quote] for your output.
    There is a preview button you can use to be sure your post is well formed before submitting.

  3. The Following User Says Thank You to jps For This Useful Post:

    willie lee (October 5th, 2012)

  4. #3
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    jps,

    Here are my class declarations:
    Scanner input = new Scanner(System.in);
    Random myRandom = new Random();
    int randomNumber;
    static int strBet = 0;
    static int strComputeWinnings = 0;
    static int strmult;
    My while loop is in the main.
    Here is my getPayMultiplier() method:
    static int getPayMultiplier(String s1, String s2, String s3)
    {
       int cherryCnt = 0;
       if(s1.equals("CHERRIES"))
       {
          return cherryCnt++;
       }
       if(s2.equals("CHERRIES"))
       {
          return cherryCnt++;
       }
       if(s3.equals("CHERRIES"))
       {
          return cherryCnt++;
       }
       if(cherryCnt == 1)
       {
          return 3;
       }
       else if(cherryCnt == 2)
       {
          return 10;
       }
       else if(cherryCnt == 3)
       {
          return 20;
       }
       else
       {
          if(s1.equals(s2) && s1.equals(s3))
          {
             if(s1.equals("BAR"))
             {
                return 35;
             }
             else if(s2.equals("7"))
             {
                return 50;
             }
          }
          return 0;
    }
    Next you will see my ComputeWinnings() method:
    static int ComputeWinnings(int bet, int mult)
    {
       String s1 = pull(); String s2 = pull(); String s3 = pull();
       ComputeWinnings = (getBet() * getPayMultiplier(s1, s2, s3));
       return ComputeWinnings(bet, mult);
    }
    I believe my problems lie somewhere within these two methods. For the "output" see my post of 04/10/2012. If you have any suggestions they would be greatly appreciated. If you need more code from my program, please let me know. Thanks again.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: computeWinnings()

    Can you post a small, complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    willie lee (October 8th, 2012)

  7. #5
    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: computeWinnings()

    Does the method getBet() prompt the user for a dollar amount? ..this method was not included in any of the posts, but seems related to your issue.
    Look at the posted while loop:
    while (getBet()=!0)//enter an amount
    {
       strBet = getBet();//enter an amount again
       String pullOne = pull();
       String pullTwo = pull();
       String pullThree = pull();
       int strMult = getPayMultiplier(pullOne,pullTwo,pullThree);
       int strComputeWinnings = getBet()*mult;//enter an amount again
       int winnings = computeWinnings()
       display(pullOne,pullTwo,pullThree,ComputeWinnings);
    }
    The only issue may be the fact that you call getBet() repeatedly rather than once and referring to the number later on inside the loop. If this is not the case, I will ask you to post the code again including all of the methods related to the problem. ie getBet() and pull()...

    On that note, I would like to post some sample pseudo code:
    while (true)
       if(getBet() equals 0
          exit the while loop, 
       otherwise deduct bet amount from player total 'cash'
     
       get a random value for each 'wheel'
       if(any of the possible winning combinations happened)
          pay the highest win amount,
       otherwise player lost this round
     
       display results of the round to the winner
    }
    Good luck with your game and let us know how it works out.

  8. The Following User Says Thank You to jps For This Useful Post:

    willie lee (October 9th, 2012)

  9. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: computeWinnings()

    Hi!

    I think the problem lies in statement:
    return cherryCnt++;
    .
    cherryCnt never gets incremented. It will always be 0. If you want to increment it, you must say
    return ++cherryCnt;
    or
    cherryCnt++;
    return cherryCnt;
    .
    Also there is a problem with
    if(cherryCnt == 1)
    It is a local variable and you set it =0 in the begining. I think it will never be 1.

  10. The Following User Says Thank You to alexdan For This Useful Post:

    willie lee (October 9th, 2012)

  11. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: computeWinnings()

    Quote Originally Posted by willie lee View Post
    ...
    Here is my getPayMultiplier() method:
    static int getPayMultiplier(String s1, String s2, String s3)
    {
       int cherryCnt = 0;
       if(s1.equals("CHERRIES"))
       {
          return cherryCnt++; //<--- From Zaphod_b: Huh?  Returns 0
       }
       if(s2.equals("CHERRIES"))
       {
          return cherryCnt++; //<--- From Zaphod_b: Huh?  Returns 0
       }
       if(s3.equals("CHERRIES"))
       {
          return cherryCnt++; //<--- From Zaphod_b: Huh?  Returns 0
       }
     
       // If there were any cherries, it never gets this far!
       if(cherryCnt == 1)
       {
          return 3;
       }
       else if(cherryCnt == 2)
       {
          return 10;
       }
       else if(cherryCnt == 3)
       {
          return 20;
       }
       else
       {
          if(s1.equals(s2) && s1.equals(s3))
          {
             if(s1.equals("BAR"))
             {
                return 35;
             }
             else if(s2.equals("7"))
             {
                return 50;
             }
          }
          return 0;
    }
    Have you actually looked at what it is doing? It makes absolutely no sense (to me, at least). It returns zero if and when it sees a cherry, without checking anything else.

    Don't you want it to be something like:
    //
    //
    Count number of cherries.
    //
    // Just count: don't return yet!
    //
     
    If the number of cherries is greater than zero
    THEN
        Calculate and return the amount for the
        number of cherries that you found.
    END IF
     
    //
    ///If we got this far, we know there were no cherries,
    // so check other possible winners:
     
     
    IF there are three Bars
    THEN
        Return amount for three Bars
    END IF
     
    //
    /// If we got this far, we know that there were no cherries,
    //  and it was not three Bars.  Maybe it's the Biggie!
     
    IF there are three Sevens
    THEN
        Return amount for three Sevens.
    END IF
     
    //
    /// If we got this far, there were no winners.
    //  Bummer!
    //
     
        return zero.
     
    // End of method

    After counting the number of cherries, you can still check the values one at a time for 1, 2, 3 just as your code did (doesn't have to be line-for-line like the pseudo-code).

    Just don't bail out before you have counted all of the cherries.

    Also I think your scheme for checking three Bars and three Sevens is fine, just as it is (doesn't have to be line-for line like the pseudo-code).




    Cheers!

    Z
    Last edited by Zaphod_b; October 7th, 2012 at 03:28 PM.

  12. The Following User Says Thank You to Zaphod_b For This Useful Post:

    willie lee (October 9th, 2012)

  13. #8
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Norm, I entered my complete program with output, but this forum says that my reply cannot be sent and I must login again and refresh the page. If you leave the page to do this all the data is lost. What is going on?

  14. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: computeWinnings()

    Sorry, I don't know how the forum's software works.

    Try it again.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #10
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Hi Norm, here we go again.
    import java.util.Scanner;
    public class NewFoothill
    {
       Scanner input = new Scanner(System.in);
       Random myRandom = new Random();
       int randomNumber;
       static int strBet = 0;
       static int ComputeWinnings = 0;
       static int mult;
     
       public static void main (String [] args) throws Exception
       {
          while (getBet() != 0)
          {
             strBet = getBet();
             String pullOne = pull();
             String pullTwo = pull();
             String pullThree = pull();
             int mult = getPayMultiplier(pullOne, pullTwo, pullThree);
             int ComputeWinnings = getBet() * mult;
             display(pullOne, pullTwo, pullThree, ComputeWinnings);
          }
          System.out.println("Thanks for playing.");
       }
     
       static int getBet()
       {
          System.out.println("Enter bet:  \n"
                                    + "To stop playing, enter 0:  ");
          Scanner userInput;
          userInput = new Scanner(System.in);
          String strBet = userInput.nextLine();
          return Integer.parseInt(strBet);
       }
     
       static String pull()
       {
          double pull = Math.random();
          if(pull < 0.25d)
          {
             return "BAR";
          }
          else if(pull < 0.50d)
          {
             return "7";
          }
          else if(pull < 0.75d)
          {
             return "CHERRIES";
          }
          else
          {
             return "SPACE";
          }
       }
     
       static int getPayMultiplier(Sring s1, String s2, String s3)
       {
          int cherryCnt = 0;
          if(s1.equals("CHERRIES"))
          {
             cherryCnt++;
          }
          if(s2.equals("CHERRIES"))
          {
             cherryCnt++;
          }
          if(s3.equals("CHERRIES"));
          {
             cherryCnt++;
          }
          if((cherryCnt > 0))
          {
             return cherryCnt++;
          }
          if(cherryCnt == 1)
          {
             return 3;
          }
          else if(cherryCnt == 2)
          {
             return 10;
          }
          else if(cherryCnt == 3)
          {
             return 20;
          }
          else
          {
             if(s1.equals(s2) && s1.equals(s3))
             {
                if(s1.equals("BAR"))
                {
                   return 35;
                }
                else if(s2.equals("7"))
                {
                   return 50;
                }
             }
             return 0;
          }
     
          static int ComputeWinnings(int bet, int mult)
          {
             String s1 = pull(); String s2 = pull(); String s3 = pull();
             ComputeWinnings = (getBet() * getPayMultiplier(s1, s2, s3));
             return ComputeWinnings(bet, mult);
          }
     
          static void display(String s1, String s2, String s3, int winnings)
          {
             System.out.println(s1 + " " + s2 + " " + s3);
             if(winnings == 0)
             {
                System.out.println("Sorry, you lose.");
             }
             else
             {
                System.out.println("You won $" + winnings);
             }
          }
       }
    Sample "output."
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    CHERRIES BAR BAR
    You won $100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter o:
    100
    SPACE 7 BAR
    Sorry, you lose.
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    SPACE CHERRIES BAR
    You won $100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    Enter bet:
    To stop playing, enter 0:
    100
    7 7 7
    You won $5000
    Enter bet:
    To stop playing, enter 0:
    0
    Thanks for playing!
    Norm, the winnings for CHERRIES is not computed correctly. BAR and 7 are computed correctly. Lastly, I need to enter my bet three times before my loop fires up. Those are my problems. I hope you can guide me in the right direction because my kids want to play this slot machine program ASAP with my money of course.

  16. #11
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Zaphod_b, thanks for your help. Your insight improved my program. I inserted:
    if((cherryCnt > 0)
    {
       return cherryCnt++;
    }
    right after the if stat.(s3.equals("CHERRIES") and it counts my cherry winnings, but not correctly. For example, my bet is $100 and I have 1 cherry it says I have won $100. It should read $300. If I have two or three cherries the program will add $100 for each of those. The BAR and "7" compute correctly. What is the error of my ways?

  17. #12
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    alexdan, you are right about my cherryCtn. I am working on it. My program counts the cherries now, but the mult does not compute correctly so the winnings that are reported are not correct(for example, bet:100, 1 cherry gives me 100, it should be 300. I posted my complete program on the forum today (09/10/12, Thia time). Have a look.

  18. #13
    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: computeWinnings()

    Any time you are testing code with random, set the seed so you can compare apples to apples. Your favorite search engine will assist in setting the seed if you do not know how.



    static int getPayMultiplier(Sring s1, String s2, String s3)
    The type declaration for s1 is missing the letter 't'. This method is also missing the final closing curly brace '}'




    while(getBet() != 0) {
       strBet = getBet();
       String pullOne = pull();
       String pullTwo = pull();
       String pullThree = pull();
       int mult = getPayMultiplier(pullOne, pullTwo, pullThree);
       int ComputeWinnings = getBet() * mult;
    You are still asking the user to enter a bet three times rather than asking once and remembering the user's entry. Assign the returned bet value to a variable and use it over again to correct this problem.


    static int ComputeWinnings(int bet, int mult) {
       String s1 = pull();
       String s2 = pull();
       String s3 = pull();
    Once you get to the compute winnings method, you ignore the pulls which occurred during game play, and you get a new value for each 'wheel' on the game. Instead you should refer to the values set on the wheel during the play to calculate the payout.


    static int ComputeWinnings(int bet, int mult) {
       String s1 = pull();
       String s2 = pull();
       String s3 = pull();
       ComputeWinnings = (getBet() * getPayMultiplier(s1, s2, s3));
       return ComputeWinnings(bet, mult);
    	}
    How will this method ever end and return a value? The recursive return statement will never allow the first game to end, if it had ever been called, which it is not. Also I want to mention at this point that there are two variables with the same name as this method. This makes the code hard to read and follow. While we are on variable names, strBet sure gives the impression that the variable will be a string, and in the method it is a string. However here again we have two variables with the same name adding to the confusion of the program.



    Take these things one step at a time. The first thing I would do is rename those variables and methods so that you can keep track of them. Then start where the program starts and use the variables in place of extra method calls. Try to get the flow moving in a forward direction. Don't rely on your existing printlns for results. Add a println every other line of code if necessary. You can always remove them later on. I sometimes make a method (seen below) just for the speed of use.
    void say(String m) {System.out.println(m);}
    Then you can just add say("variableName: " + variableName);

  19. #14
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: computeWinnings()

    Quote Originally Posted by willie lee View Post
    Zaphod_b, thanks for your help. Your insight improved my program. I inserted:
    if((cherryCnt > 0)
    {
       return cherryCnt++;
    }
    right after the if stat.(s3.equals("CHERRIES") and it counts my cherry winnings, but not correctly.
    So, take a lesson: If doing that does the wrong thing, then (wait for it...wait for it...OK, here it is...) don't do that!

    Just use your cascade of if(){}else{} statements to return the correct amount. I mean, you don't need a separate test for cherryCnt > 0 in your code. Just go through the same motions that you had in mind in the first place.

    Cheers!

    Z

  20. The Following User Says Thank You to Zaphod_b For This Useful Post:

    willie lee (October 22nd, 2012)

  21. #15
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Norm, I enter my program with sample output on 09/10/2012. Have you had a chance to look at it and my comments?

  22. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: computeWinnings()

    Did you make the changes that were suggested by the other posters? Can you post the new version and its output?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #17
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Zaphod_b, yes you were correct. I got rid of the sep. test. The mult now works corectly for the amt. of "CHERRIES" shown. However, I still have a problem in that if "CHERRIES" does not appear in the 1st position there should not be any winnings. Hence, if I get the following: "7" "BAR" "CHERRIES" my program computes winnings for 1 cherry. So I need to figure out a way to compute winnings on the following only: "CHERRIES" [not cherries] [any]......"CHERRIES" "CHERRIES" [any]......."CHERRIES" "CHERRIES" "CHERRIES". Again, thanks very much for your kind help.

  24. #18
    Member
    Join Date
    Feb 2012
    Posts
    35
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: computeWinnings()

    Norm, thanks for your reply. Yes, I made the changes and they helped, but I still have a problem in that my program counts cherry winning when it should not. If "CHERRIES" does not appear in the 1st position winnings should not be computed for "CHERRIES". Here is the "new version" of my static int getPayMultiplier(String s1, String s2, String s3)
    static int getPayMultiplier(String s1, String s2, String s3)
    {
       int cherryCnt = 0;
       if(s1.equals("CHERRIES"))
       {
          cherryCnt++;
       }
       if(s2.equals("CHERRIES"))
       {
          cherryCnt++;
       }
       if(s3.equals("CHERRIES"))
       {
          cherryCnt++;
       }
       if(cherryCnt == 1)
       {
          return 3;
       }
       if(cherryCnt == 2)
       {
          return 10;
       }
       if(cherryCnt == 3)
       {
          return 20;
       }
       else
       {
          if(s1.equals(s2) && s1.equals(s3))
          {
             if(s1.equals("BAR"))
             {
                return 35;
             }
             else if(s2.equals("7"))
             {
                return 50;
             }
          }
       }
       return 0;
    }
    Enter bet:
    To stop playing, enter 0:
    100
    7 BAR CHERRIES
    You won $300
    The rest of my program remains the same.
    Last edited by willie lee; October 22nd, 2012 at 08:22 AM.

  25. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: computeWinnings()

    Please post the full program and its output that shows the problem. Be sure to add comments to the output describing what it should be.
    If you don't understand my answer, don't ignore it, ask a question.