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
Code :
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:
Code :
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:
Code :
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?
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: http://www.javaprogrammingforums.com...s/codetags.jpg
Re: Help with slot machine
You are getting 4 matches each time because pvalue1, pvalue2, pvalue3 and pvalue4 always ends up as 0
Re: Help with slot machine
thank you very much for your help
do you know what I can do to fix it?
Re: Help with slot machine
It doesn't look like WackySlots.randomValues(); is ever called to generate those values?
Re: Help with slot machine
How about adding it into your while loop?
Code :
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.
Re: Help with slot machine
Thank you, I'll try that now.
Re: Help with slot machine
Yes! It works! Thank you so much for your help, I appreciate it very much!
Re: Help with slot machine
:D I'm glad I could help.