# Thread: problem with this program

1. Member
Join Date
Oct 2011
Location
North Carolina
Posts
5
Rep Power
0

## problem with this program

total after winning amount is not right . plz help me.
The game starts with the user having a balance of \$10.00.
The program asks the user how much he/she wishes to bet.
If the user enters a number less than zero, or greater than the amount of money in the user's balance, you should repeat step 2.
If the user entered a bet of zero, go to step 9 below.
After a valid bet has been entered, the program should randomly select three numbers from 0-9 and display them on the screen (with some space between for neatness).
If a winning combination is produced, the program should calculate and display the amount of money the user has won. (If there is no win, then do not display anything)
Display the remaining balance. The balance at this point will be the original balance, minus the amount of the bet, plus any winnings.
If the user still has money remaining, the program should ask the user how much he/she wishes to bet.
If the user enters a number less than zero, or greater than the amount of money in the user's balance, you should repeat step 7.
If the user does not have money remaining, you should display a message stating that the user has run out of money, and then go to step 9 below.
Repeat steps 3-8.
At the conclusion of the program, you should display:
The amount of money the user has won or lost.
The final balance that the user will be cashing out.

Java Code:
```import java.util.*;
public class SlotMachine {

public static int balance = 10;
public static void main(String[] args) {

Scanner kbd = new Scanner(System.in);
int slot1, slot2, slot3;
Random generator = new Random();
slot1 = generator.nextInt(10);
slot2 = generator.nextInt(10);
slot3 = generator.nextInt(10);
int winAmount = 0, bet = 0;
//slot1 = 3;
//slot2 = 7;
//slot3 = 9;

System.out.println("Starting balanceance = \$10.00");
System.out.println("Enter your bet (or 0 to quit): ");
bet = kbd.nextInt();
while (bet > 0 || winAmount <=0)
{
{
System.out.println("Slot result: \n" + slot1 + "  " + slot2 + "  " + slot3);

if (slot1 == slot2 && slot1 == slot3);
{
winAmount = ((slot1 + 1) * bet);

}
if ((slot1 == slot2 && slot1 != slot3)|| (slot2 == slot3 &&
slot2 != slot1) || (slot3 == slot1 && slot3 != slot2));
{
if (slot1 == slot2)
{
winAmount = ((bet * slot1)/2);
balance = balance + winAmount;
System.out.println("You have won: \$" + winAmount);
System.out.println("Balance: \$" + (balance + winAmount));

}

else if (slot2 == slot3)
{
winAmount = ((bet * slot2)/2);
balance = balance + winAmount;
System.out.println("You have won: \$" + winAmount);
System.out.println("Balance: \$" + (balance + winAmount));

}
else if (slot1 == slot3)
{
winAmount = ((bet * slot1)/2);
balance = balance + winAmount;
System.out.println("You have won: \$" + winAmount);
System.out.println("Balance: \$" + (balance + winAmount));

}
}
if (slot1 != slot2 && slot1 != slot3)
{
balance = balance - bet;
System.out.println("You have won: \$" + winAmount);
System.out.println("Balance: \$" + (balance + winAmount));
}
System.out.println("Enter your bet (or 0 to quit): ");
bet = kbd.nextInt();
}
}
}
}```
Last edited by pbrockway2; 10-29-2011 at 06:36 AM. Reason: code tags corrected

2. ## Re: problem with this program

3. Member
Join Date
Oct 2011
Location
North Carolina
Posts
5
Rep Power
0

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,561
Rep Power
11

## Re: problem with this program

When you post code use the "code" tags: [code] and [/code] rather than [color]. This will mean the indentation is preserved when the code is displayed here. Also it's a good idea to use spaces (eg 4) rather than tabs as the latter tend to be rather big when rendered by a web browser.

total after winning amount is not right
Your idea of hardcoding the slotN variables is a good one for illustrating how the program is not behaving correctly. But you will need to say what the winning amount should be! As well as what amount (if any) is printed when you run the program.

5. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: problem with this program

Look at how you're printing winAmount.
First you calculated the value of winAmount with this:

Java Code:
`winAmount = ((bet * slot1)/2);`
So there is no need to recalculate it when you print it.

Java Code:
`System.out.println("Balance: \$" + (balance + winAmount));`
all you need is to print the balance

--------------------------------------------------------------------------------------------------------------------------------------------------

As for when the player has lost
Java Code:
```  if (slot1 != slot2 && slot1 != slot3)
{
balance = balance - bet;
System.out.println("You have won: \$" + winAmount);
System.out.println("Balance: \$" + (balance));
}```
You make the system print "You have won" when they haven't won.
So change that.
Also, when you loose, you don't loose your winAmount, you loose your bet right?
You're calculating balance correctly but you're just printing it out wrong.
Java Code:
` System.out.println("You have won: \$" + winAmount);`
--------------------------------------------------------------------------------------------------------------------------------------------------

Now, every time you try and bet again, you get the same results.
This is because you start the program out by calculating the slot results, and you never change them. This needs to be calculated each time a new bet is made, and a new bet is made inside your while loop...
Java Code:
```Random generator = new Random();
slot1 = generator.nextInt(10);
slot2 = generator.nextInt(10);
slot3 = generator.nextInt(10)```
--------------------------------------------------------------------------------------------------------------------------------------------------

Also, why are you asking the user for their bet in two different places? Why not just ask them once at the beginning of the while loop?

Java Code:
```                 System.out.println("Enter your bet (or 0 to quit): ");
bet = kbd.nextInt();```
--------------------------------------------------------------------------------------------------------------------------------------------------
Last edited by Fubarable; 10-30-2011 at 11:40 PM.

6. ## Re: problem with this program

One of your biggest problems is with a mis-placed semicolon.

This statement:

Java Code:
```                if (slot1 == slot2 && slot1 == slot3);
{
winAmount = ((slot1 + 1) * bet);

}```
is the same as doing this:

Java Code:
```                if (slot1 == slot2 && slot1 == slot3)
{
// do nothing
}

// this code below runs all the time regardless of the if condition above.
{
winAmount = ((slot1 + 1) * bet);

}```
The solution is to get rid of the aberrant semicolon:

Java Code:
```                if (slot1 == slot2 && slot1 == slot3) // *** no semi colon here!
{
winAmount = ((slot1 + 1) * bet);

}```
You have several other if blocks with the same problem.

7. Member
Join Date
Oct 2011
Location
North Carolina
Posts
5
Rep Power
0

## Re: problem with this program

thanks everyone for help.

everything work good except one.

when total amount goes to negative program not stop or show error

8. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: problem with this program

At the beginning of the while loop, do something to check if user's value is negative (less than 0) , then have it do something else and end the loop.
Hint: you can end loops with this:

Java Code:
`break;`
Also if you wanna do something like: "You are 20 dollars in debt"
It will print -20 if you just print their balance
You could just multiply it by -1
-20 x -1 = 20

#### Posting Permissions

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