5 Replies - 2944 Views - Last Post: 07 April 2007 - 07:07 PM Rate Topic: -----

#1 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

my little slot machine game help (loops, methods, if/else)

Post icon  Posted 27 March 2007 - 03:33 PM

can someone help me with my slot machine game? I am trying to make it so a user puts in an initial bet which has to be divisible by .25(that part works right) and then the user can place a bet of any number as long as it is lower than the initial and higher than 25 cents. I can get it to work for the most part, I am having problems at the end of the game;

1st.
if the user inputs no when asked if they want to play again my game is suppose to go through a series of if statements depending the users outcome. My problem is that if i input "no" the game displays the correct message but once it is done displaying the message it starts the game over, i need it to end

2nd.
my game is suppose to stop once the number bet reaches zero and not allow the user to continue the bet if it is going to be below zero. however on mine if my bet continues regardless and if it is <= zero it repeats the game I want it to end.

(I am figuring it is a loop problem, but I am terrible with loops) PLEASE HELP!!
import javax.swing.JOptionPane;  //importing the JOptionPane option
import java.util.Random;		 //importing random numbers for slot machine
import java.text.DecimalFormat;  //importig decimal formatter option

public class PRACTICE_3  // Jay Masi, jmm72@pitt.edu, M W 3:00 - 4:15
{
  public static void main (String [] args) //Main method
  {
	Random questions = new Random();					   //Establishing the random question variable
	DecimalFormat formatter = new DecimalFormat("#0.00");  //Allowing the pecentage to be confined
	int sign1 = 0;  //First number on machine  
	int sign2 = 0;  //Second number on machine
	int sign3 = 0;  //Third number on machine
	double number = 0.0;  //Inital bet
	double winnings = 0.0;  //Total winnings
	double balance = 0.0;  //Variable for the updateBalance method
	double bet = 0.0;  //Variable 
	String input;
	String ask;
	
	
	  
	input = JOptionPane.showInputDialog("Welcome to Jay's Slot Machine \n\nPlace your bet");
	number = Double.parseDouble(input);
	
   
	while(number%.25!=0)
	{
	input = JOptionPane.showInputDialog("Please Enter an amount divisible by $.25");
	number = Double.parseDouble(input);
	}
	
	winnings = number;
	
	 input = JOptionPane.showInputDialog("your balance is: $"+formatter.format(winnings)+"\nHow much do you want to bet?");
	 bet = Double.parseDouble(input);
	 
	 /* Declaring the while loop in order to play the slots */
	 while(bet >= .25 && bet <= number)
	 {
	   // Figuring out the random numbers for the slots
	   sign1 = (Math.abs(questions.nextInt())%6);
	   sign2 = (Math.abs(questions.nextInt())%6);
	   sign3 = (Math.abs(questions.nextInt())%6);
	   JOptionPane.showMessageDialog(null, sign1+" - "+sign2+" - "+sign3);
	  
	   
	   // Calling the method to receive the results
	   balance = updateBalance( sign1, sign2, sign3, bet);
	   
	   // Keeping track of the total winnings
	   winnings += balance;
	   
	   if(bet > winnings)
		 input = JOptionPane.showInputDialog("Your balance is $"+formatter.format(winnings)+"\nPlease enter another bet less than or equal to your balance that is divisible by $.25");
	   
	 if(winnings > 0)  
	 input = JOptionPane.showInputDialog("Your Balance is $"+formatter.format(winnings)+"\nDo you want to play again?", "yes or no");
	 
	 
	 
	 if(input.equals("no"))
	   if(winnings > number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number));
	   if(winnings ==  number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even");
	   if(winnings < number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number));
	  }
	   System.exit(0);
	 }

  
  // The udateBalance method
  public static double updateBalance(int s1, int s2, int s3, double B)
  {
	
	 
	DecimalFormat formatter = new DecimalFormat("#0.00");  // Declaring the decimal format variable
	double n = 0;  // return variable
	
	// Payout rules
	if (s1 == s2 && s2 == s3)
	{
	  if (s1 == 0){
		n = (0 * B);
		JOptionPane.showMessageDialog(null, "You Got All 0's \nYou Won $"+formatter.format(n));}
	  if (s1 == 1){
		n = (1 * B);
		JOptionPane.showMessageDialog(null, "You Got All 1's \nYou Won $"+formatter.format(n));}
	   if (s1 == 2){
		n = (2 * B);
		JOptionPane.showMessageDialog(null, "You Got All 2's \nYou Won $"+formatter.format(n));}
	   if (s1 == 3){
		n = (3 * B);
		JOptionPane.showMessageDialog(null, "You Got All 3's \nYou Won $"+formatter.format(n));}
	   if (s1 == 4){
		n = (4 * B);
		JOptionPane.showMessageDialog(null, "You Got All 4's \nYou Won $"+formatter.format(n));}
	   if (s1 == 5){
		n = (400 * B);
		JOptionPane.showMessageDialog(null, "JACKPOT!!!!! \nYou Won $"+formatter.format(n));}
	}
		 else
		 {
	
	  if (s1 == s2 && s2 != s3 || s1 == s3 && s1 != s2 || s2 == s3 && s2 != s1)
	 {
	  n = b;
	  JOptionPane.showMessageDialog(null, "You Got 2 Of Them \nYou Recieved Back Your $"+formatter.format(n));
	  }
	  else
	  {
	  n = -b;
	  JOptionPane.showMessageDialog(null, "You Didn't Get Any Right You Lost $"+formatter.format(Math.abs(n)));
	
	  }
		}
  return n;
  }


Is This A Good Question/Topic? 0
  • +

Replies To: my little slot machine game help (loops, methods, if/else)

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: my little slot machine game help (loops, methods, if/else)

Posted 27 March 2007 - 04:52 PM

Here's the part of your problem that jumped out at me:
  while(...)
  {
	.
	.
	.
	if(input.equals("no"))
	   if(winnings > number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number));
	   if(winnings ==  number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even");
	   if(winnings < number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number));
  }
  System.exit(0);
	 }


What you want is for your first if statement to cover the next three statements. So basically,
if(input.equals("no"))
then check if(winnings > number)
and check if(winnings == number)
and check if(winnings < number)

The way you have it set up right now, if the first if statement is true, then only the second if statement will be checked. To fix this, block off the if statement with brackets:
if(input.equals("no"))
{
   if(winnings > number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number));
   if(winnings ==  number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even");
   if(winnings < number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number));
}



This leads me to your next problem: your statement System.exit(0) is not located inside of the while loop. As far as I can see, your loop will never be false, so in essence, you have an infinite loop. There are three ways to combat this.
1- you could change your while statement
2- you could use the 'break' statement in your if block
3- you could exit your program while you are still in your while loop

These suggestions are in order of descending goodness. So, ideally, what you want to do is fix your while loop to be something that will eventually be false. I haven't looked over you code to know what that condition may be, so I'll give you the quick and messy way.

For the most part, using break to get out of a loop is frowned upon in Java. However, I'm learning C right now, and my professor encourages me to do this, so, here goes:

In your if statement at the bottom of the loop, you ask the user whether or not they want to continue. If they say 'no', then you do not want to go through the loop again. The way to do this instantly is to write:
break;

Doing this will get you out of your while loop and will allow you to run the code that comes after it. Namely:
System.exit(0);

It's a nice and easy fix:
if(input.equals("no"))
{
   if(winnings > number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number));
   if(winnings ==  number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even");
   if(winnings < number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number));

   break;	  //that's it
}



Now this is not ideal, but it gets the job done.

In the future, you may want to fix your while loop. I would suggest adding the following condition:
while( . . .  && input.equalsIgnoreCase("no"))



This will basically do the same thing as your if statement, in that it wont let the loop iterate through again, but I'll leave you to play around with that one.

Good luck.

This post has been edited by keems21: 27 March 2007 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 97
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: my little slot machine game help (loops, methods, if/else)

Posted 27 March 2007 - 05:03 PM

when you get this working can you post your code, little bit more advanced than my stage and would like to take it apart. thanks.
Was This Post Helpful? 0
  • +
  • -

#4 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: my little slot machine game help (loops, methods, if/else)

Post icon  Posted 28 March 2007 - 01:01 PM

Thank you so much that helped a lot now I am having two more problems.
1st.
once my bet reaches zero I want the game to stop only it restarts.

2nd.
The other thing is, say I have a balance of $1.00 but my bet is $2.00 I want the game to pop up a message and ask the user to input another number that is less than or equal to the balance (winnings) however, the message does pop up but if I input a number that is higher than the balance the program goes back to asking if I want to play again. I want it so that if I enter a number higher than the blance the program will keep asking me enter a smaller number. To even further that if I were to input a number equal to the balance and I lose I want to program to end like stated in my first problem.

thank you

.
.
.

input = JOptionPane.showInputDialog("your balance is: $"+formatter.format(winnings)+"\nHow much do you want to bet?");
bet = Double.parseDouble(input);

/* Declaring the while loop in order to play the slots */
while(bet >= .25 && bet <= number)
{
// Figuring out the random numbers for the slots
sign1 = (Math.abs(questions.nextInt())%6);
sign2 = (Math.abs(questions.nextInt())%6);
sign3 = (Math.abs(questions.nextInt())%6);
JOptionPane.showMessageDialog(null, sign1+" - "+sign2+" - "+sign3);


// Calling the method to receive the results
balance = updateBalance( sign1, sign2, sign3, bet);

// Keeping track of the total winnings
winnings += balance;

if(bet > winnings) //THIS IS WHERE I NEED HELP
input = JOptionPane.showInputDialog("Your balance is $"+formatter.format(winnings)+"\nPlease enter another bet less than or equal to your balance that is divisible by $.25");

if(winnings > 0)
input = JOptionPane.showInputDialog("Your Balance is $"+formatter.format(winnings)+"\nDo you want to play again?", "yes or no");



if(input.equals("no"))
{
if(winnings > number)
JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number));
if(winnings == number)
JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even");
if(winnings < number)
JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number));
break;
}
}
System.exit(0);
}
Was This Post Helpful? 0
  • +
  • -

#5 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: my little slot machine game help (loops, methods, if/else)

Posted 28 March 2007 - 03:40 PM

Alright, well I'm not going to spell this one out for you. But I will give you a little more guidance. Trust me, it will be worth it in the end if you figure this out on your own.

So these are your problems:

Quote

Thank you so much that helped a lot now I am having two more problems.
1st.
once my bet reaches zero I want the game to stop only it restarts.

2nd.
The other thing is, say I have a balance of $1.00 but my bet is $2.00 I want the game to pop up a message and ask the user to input another number that is less than or equal to the balance (winnings) however, the message does pop up but if I input a number that is higher than the balance the program goes back to asking if I want to play again. I want it so that if I enter a number higher than the blance the program will keep asking me enter a smaller number. To even further that if I were to input a number equal to the balance and I lose I want to program to end like stated in my first problem.


Whenever you have something like this, there are two things you need to this of:
1.) How can I describe this situation with the code that I've written so far?
2.) When will this situation occur?

#2 is easy for both situations: both occur in your while loop. You may need to tweak where you put it in the while loop, but that is a detail that you can figure out with a little trial and error.

Now for #1. Try writing what you just said is psudocode:
if(bet == 0)
   exit //could you use the break statement again here?



and
if(balance < bet)
  pop up message



One more suggestion: the best debugging you can do at this point is, instead of running the program over and over again with given situations, come up with a theoretical situation in your mind then read through the code that would execute for the given situation.

This may sound a little confusing, so let me give you an example:
-you have a bank of $400
-a prompt comes up
----you chose to bet $20
-you enter the while loop: while(bet >= .25 && bet <= number)
-your balance is updated
-winnings are updated
-balance is shown in a window, you're asked for another bet
----you chose $20 again
-you're asked if you want to play again
----you put in 'yes'
.
.
.

Now every extra indented statement is a decision that the person playing the game can make. What if, for the first decision they choose to bet $20000 when there is only $400 in the bank? Then which if statements are run and what decision can be made. After you play thourgh this, test it out in the real game. Enter a bet of $20000 and see if what happens is what you expect.

This is the game the programmers constantly play with themselves: did what I expect to happen actually happen?
-If it did, then that's awesome.
-If it didn't, then why didn't it?

This is what debugging is all about.

Now get to it. Come back with any problems that you're having too much trouble figuring out.

Good luck.

This post has been edited by keems21: 28 March 2007 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 BudLight412  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-January 07

Re: my little slot machine game help (loops, methods, if/else)

Posted 07 April 2007 - 07:07 PM

hey man im sorry you are probably way past this by now i forgot to post it up. here it is if you still need it:
import javax.swing.JOptionPane;  //importing the JOptionPane option
import java.util.Random;		 //importing random numbers for slot machine
import java.text.DecimalFormat;  //importig decimal formatter option

public class Jay_Masi_p3  // Jay Masi, jmm72@pitt.edu, M W 3:00 - 4:15
{
  public static void main (String [] args) //Main method
  {
	Random questions = new Random();					   //Establishing the random question variable
	DecimalFormat formatter = new DecimalFormat("#0.00");  //Allowing the pecentage to be confined
	int sign1 = 0;  //First number on machine  
	int sign2 = 0;  //Second number on machine
	int sign3 = 0;  //Third number on machine
	double number = 0.0;  //Inital bet
	double winnings = 0.0;  //Total winnings
	double balance = 0.0;  //Variable for the updateBalance method
	double bet = 0.0;  //Variable 
	String input;  //The String used throughtout the program
	
	
	
	  
	input = JOptionPane.showInputDialog("Welcome to Jay's Slot Machine \n\nPlace your maximum bet ($)");
	number = Double.parseDouble(input);
	
   
	while(number%.25!=0)
	{
	input = JOptionPane.showInputDialog("Please Enter an amount divisible by $.25");
	number = Double.parseDouble(input);
	}
	
	winnings = number;
	
	 input = JOptionPane.showInputDialog("Your balance is: $"+formatter.format(winnings)+"\nHow much do you want to bet? ($)");
	 bet = Double.parseDouble(input);
	 
	 /* Declaring the while loop in order to play the slots */
	 while(bet >= .25 && bet <= winnings)
	 {
	   // Figuring out the random numbers for the slots
	   sign1 = (Math.abs(questions.nextInt())%6);
	   sign2 = (Math.abs(questions.nextInt())%6);
	   sign3 = (Math.abs(questions.nextInt())%6);
	   JOptionPane.showMessageDialog(null, sign1+" - "+sign2+" - "+sign3);
	  
	   
	   // Calling the method to receive the results
	   balance = updateBalance( sign1, sign2, sign3, bet);
	   
	   // Keeping track of the total winnings
	   winnings += balance;
	   
	   if(winnings <= 0) // Telling user that they lost their money if they hit zero
	   {
		 JOptionPane.showMessageDialog(null, "You lost all of your money");
		 break;
	   }
		  
		 
	 // Asking the user if they would like to play again
	 if(winnings > 0)  
	 input = JOptionPane.showInputDialog("Your Balance is $"+formatter.format(winnings)+"\nDo you want to play again?", "yes or no");
	 
	 // Displaying the final messages if the user decides to stop playing 
	 if(input.equals("no"))
	 {
	   if(winnings > number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Won $"+formatter.format(winnings-number)+" from your original bet of $"+formatter.format(number)+"\nGrand Total $"+formatter.format(winnings));
	   if(winnings ==  number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Broke Even \nYour Grand Total $"+formatter.format(winnings));
	   if(winnings < number)
		 JOptionPane.showMessageDialog(null, "Thank you for Playing \nYou Lost $"+formatter.format(number-winnings)+" from your original bet of $"+formatter.format(number)+"\nGrand Total $"+formatter.format(winnings));
	   
	   break; // stopping the loop from continuing on
	 }
	  while(bet > winnings){
		 input = JOptionPane.showInputDialog("Your balance is $"+formatter.format(winnings)+"\nPlease enter another bet less than or equal to "+formatter.format(winnings)+" that is divisible by $.25");
		 bet = Double.parseDouble(input);}
	   
	 }  
	 System.exit(0);
  }
	 

  
  // The udateBalance method
  public static double updateBalance(int s1, int s2, int s3, double B)
  {
	 
	DecimalFormat formatter = new DecimalFormat("#0.00");  // Declaring the decimal format variable
	double n = 0;  // return variable
	
	// Payout rules
	if (s1 == s2 && s2 == s3)
	{
	  if (s1 == 0){
		n = (0 * B);
		JOptionPane.showMessageDialog(null, "You Got All 0's \nYou Won $"+formatter.format(n));}
	  if (s1 == 1){
		n = (1 * B);
		JOptionPane.showMessageDialog(null, "You Got All 1's \nYou Won $"+formatter.format(n));}
	   if (s1 == 2){
		n = (2 * B);
		JOptionPane.showMessageDialog(null, "You Got All 2's \nYou Won $"+formatter.format(n));}
	   if (s1 == 3){
		n = (3 * B);
		JOptionPane.showMessageDialog(null, "You Got All 3's \nYou Won $"+formatter.format(n));}
	   if (s1 == 4){
		n = (4 * B);
		JOptionPane.showMessageDialog(null, "You Got All 4's \nYou Won $"+formatter.format(n));}
	   if (s1 == 5){
		n = (400 * B);
		JOptionPane.showMessageDialog(null, "JACKPOT!!!!! \nYou Won $"+formatter.format(n));}
	}
		 else
		 {
	
	  if (s1 == s2 && s2 != s3 || s1 == s3 && s1 != s2 || s2 == s3 && s2 != s1)
	 {
	  n = b;
	  JOptionPane.showMessageDialog(null, "You Got 2 Of Them \nYou Won $"+formatter.format(n));
	  }
	  else
	  {
	  n = -b;
	  JOptionPane.showMessageDialog(null, "You Didn't Get Any Right You Lost $"+formatter.format(Math.abs(n)));
	
	  }
		}
  return n;
  }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1