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

Thread: Logical error on implementing slot machine using java.util.Random

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logical error on implementing slot machine using java.util.Random

    I'm having trouble getting the number of matches to work correctly.

    I've imported java.util.Random

    Then I use this to generate a random number from 1000-9999

    public void randomValues(){
    Random Generator = new Random();
    value = 1000 + Generator.nextInt(9000);
    playerValue = 1000 + Generator.nextInt(9000);

    then to count the number of matches I seperate the four digit number into seperate single digits:

    public void payout(double coin){
    double currentPayout;
    int value1 = value/1000;
    value = value %1000;
    int value2 = value/100;
    value = value %1000 % 100;
    int value3 = value/10;
    value = value % 1000 % 100 % 10;
    int value4 = value;
     
    int pvalue1 = playerValue/1000;
    playerValue = playerValue % 1000;
    int pvalue2 = playerValue/100;
    playerValue = playerValue % 1000 % 100;
    int pvalue3 = playerValue/10;
    playerValue = playerValue %1000 % 100 % 10;
    int pvalue4 = playerValue;

    and to calculate the number of matches I use this:

    int matches = 0;
     
    if (value1 == pvalue1)
    matches=matches+1;
    if (value2 == pvalue2)
    matches = matches+1;
    if (value3 == pvalue3);
    matches = matches+1;
    if (value4 == pvalue4);
    matches = matches+1;

    however, when I run the program it says there are four matches every single time.
    I'm not sure what I'm doing wrong, can someone please help me?


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with slot machine

    Hello Jackty.

    Can you please post your entire code for me to look at? Thanks..

    Note: When posting code in the forums, make sure you use the code tags:
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with slot machine

    You are getting 4 matches each time because pvalue1, pvalue2, pvalue3 and pvalue4 always ends up as 0
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with slot machine

    thank you very much for your help

    do you know what I can do to fix it?

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with slot machine

    It doesn't look like WackySlots.randomValues(); is ever called to generate those values?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with slot machine

    How about adding it into your while loop?

         while (coin != -1){
             WackySlots.randomValues();
             machine.deposit(coin);
             //WackySlots.showStatus();
             System.out.print("Deposit a coin (1, 5, 10, 25) or -1 to quit:");
             coin = input.nextInt();
             }
    Now we have those values being generated each time.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with slot machine

    Thank you, I'll try that now.

  8. #8
    Junior Member
    Join Date
    Feb 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with slot machine

    Yes! It works! Thank you so much for your help, I appreciate it very much!

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with slot machine

    I'm glad I could help.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Java error "could not create java virtual machine"
    By aubrey4444 in forum Java Theory & Questions
    Replies: 17
    Last Post: October 3rd, 2010, 12:51 PM
  2. Moving MySql Database from one machine to another machine
    By vaishali in forum JDBC & Databases
    Replies: 5
    Last Post: July 21st, 2010, 01:21 AM
  3. need help with Vending Machine code...
    By mia_tech in forum Loops & Control Statements
    Replies: 3
    Last Post: April 20th, 2009, 05:24 AM
  4. Replies: 6
    Last Post: November 14th, 2008, 02:09 PM

Tags for this Thread