CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    12

    help with slot machine

    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
    Join Date
    Feb 2008
    Posts
    966

    Re: help with slot machine

    You have a semicolon ( at the end of your if statements. That is causing the matches = matches + 1; to always execute.

    Make it look like:
    Code:
    		if (value1 == pvalue1)
    			matches=matches+1;
    		if (value2 == pvalue2)
    			matches = matches+1;
    		if (value3 == pvalue3)
    			matches = matches+1;
    		if (value4 == pvalue4)
    			matches = matches+1;

  3. #3
    Join Date
    Feb 2009
    Posts
    12

    Re: help with slot machine

    I removed the semicolons from the if statements but it is still getting 4 matches every time.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: help with slot machine

    Did you recompile your code before running it? I've tried it and it works for me.

    Simplicity is the ultimate sophistication...
    L. Da Vinci
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Feb 2009
    Posts
    12

    Re: help with slot machine

    Yes, I'm using eclipse which compiles the program before you run it every time.

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: help with slot machine

    Clean your project and try again. Click on the Project menu item up top and chose "Clean". This will remove all of the .class file and then it can be compiled clean. It is working for me as well.

  7. #7
    Join Date
    Feb 2009
    Posts
    12

    Re: help with slot machine

    I cleaned the project, recompiled it, and ran it but it is till coming up with 4 matches every time. I'm not sure what I'm doing wrong. Maybe it's a different piece of code that is causing this problem?

  8. #8
    Join Date
    Feb 2008
    Posts
    966

    Re: help with slot machine

    Compare your code to mine, you may be doing something else (since all you gave us were tidbits of code and not all of it).

    Code:
        public static void main(String args[]) {
            Random Generator = new Random();
            int value = 1000 + Generator.nextInt(9000);
            int playerValue = 1000 + Generator.nextInt(9000);
    
            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;
            System.out.println("value1="+value1+" ; value2="+value2+" ; value3="+value3+" ; value4="+value4);
            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;
            System.out.println("pvalue1="+pvalue1+" ; pvalue2="+pvalue2+" ; pvalue3="+pvalue3+" ; pvalue4="+pvalue4);
            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;
            
            System.out.println("Matches: " + matches);
        }

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured