-
February 24th, 2009, 01:06 PM
#1
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?
-
February 24th, 2009, 02:02 PM
#2
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;
-
February 24th, 2009, 02:09 PM
#3
Re: help with slot machine
I removed the semicolons from the if statements but it is still getting 4 matches every time.
-
February 24th, 2009, 02:31 PM
#4
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.
-
February 24th, 2009, 02:36 PM
#5
Re: help with slot machine
Yes, I'm using eclipse which compiles the program before you run it every time.
-
February 24th, 2009, 03:25 PM
#6
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.
-
February 24th, 2009, 03:28 PM
#7
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?
-
February 24th, 2009, 03:50 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|