if statement.

Page 1 of 1

7 Replies - 2085 Views - Last Post: 13 December 2007 - 06:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=39271&amp;s=2762a23beaa90a0a5595e4d825f32cb8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kealh

Reputation: 0
• Posts: 14
• Joined: 06-December 07

if statement.

Posted 10 December 2007 - 11:08 PM

Hello, I am suppose to write a slot machine code. It will have to generate 3 numbers from the range 1 to 3. Also a prompt allow player to input certain amount to play. Ex 5 dollar. When play the number generates randomly and if all three number are alike then the amount double 5+5 = 10. However if the numbers are not alike the amount of money is lost by 2 5-2=3. And the player continue to play until he lost all his money, and the code will said "Cant Play No More" Also the player is able to have a choice if he wants to stop and take out his money.

I hope i explained throughly enough. But please help. And We can only use if statement, loop, Randomnumber, Array, and because of my school status our computers are not well updated so Java that are above 1.4 such as Scanner cant be use.

While i already did most of the part. I'm still missing the part where player get to chose "Cant play anymore" or he wants to stop and take out his money. My teacher said everything has to be in a if statement, but i have no clue how. Or if there any other way?

```import javax.swing.*;
import java.text.NumberFormat;
import java.util.Random;

public class SlotMachine
{
public static void main (String [] args)

{

String input;

int amount;

int snum, fnum, tnum;
JFrame frame= new JFrame ();
Random r = new Random ();

snum = r.nextInt(3 - 1 + 1)+1;
fnum = r.nextInt(3 - 1 + 1)+1;
tnum = r.nextInt(3 - 1 + 1)+1;

input = JOptionPane.showInputDialog("Enter the amount of money you wish to play");
amount =Integer.parseInt(input);

System.out.println(snum + " "+ fnum + " "+ tnum +" ");

if (snum == fnum && fnum == tnum)
{
amount = amount * 2;
}
else
{
amount = amount - 2;
}
}
}
```

Use tags

Is This A Good Question/Topic? 0

Replies To: if statement.

#2 cfoley

• Cabbage

Reputation: 2391
• Posts: 5,016
• Joined: 11-December 07

Re: if statement.

Posted 11 December 2007 - 07:11 AM

The code you listed above lets the user play the slot machine once. To let him play it several times you need to use a loop. Since you don't know (and can't calculate) how many times the user will want to play, the best type of loop is a while loop:

```// Set Up Game
while ( condition ) {
// Play Game
// Ask user if he wants to continue
} // while end

```

You've already got the set up game and the play game code written but jumbled up slightly. Once you consider which belongs where it'll be obvious which part needs to be moved.

You already know how to ask a question and use an if statement to decide what to do with the input so I'll leave that to you.

The final part is the condition. The game continues while there is enough money in the machine. As a hint, if the user takes out his money, there won't be enough money left.

#3 kealh

Reputation: 0
• Posts: 14
• Joined: 06-December 07

Re: if statement.

Posted 12 December 2007 - 09:46 PM

Ok i did as you said. And it works fine. But still i have one problem. For like if the amount is 1 or 0 is left i just want the program to said Game Over and Here is your money while the player wanted to go on but only have 1 dollar. Also i try to put if(amount <=1){
System.out.print(" Game over"); in the code which in end print out Game over with Game on. I just want Game over and that it program done.

import javax.swing.*;
import java.text.NumberFormat;
import java.util.Random;

public class SlotMachine
{
public static void main (String [] args)

{

String input;
final int SENTINAL = 0;
double amount;
int num;
int snum, fnum, tnum;
JFrame frame= new JFrame ();
Random r = new Random ();
NumberFormat money = NumberFormat.getCurrencyInstance();

input = JOptionPane.showInputDialog("Enter the amount of money you wish to play");
amount =Integer.parseInt(input);
while (amount > 1){
snum = r.nextInt(3 - 1 + 1)+1;
fnum = r.nextInt(3 - 1 + 1)+1;
tnum = r.nextInt(3 - 1 + 1)+1;

System.out.println(snum + " " + fnum + " " + tnum + " ");

if( snum == fnum && fnum==tnum){
amount = amount * 2;

} else{
amount = amount -2;

if(amount <=1){
System.out.print(" Game over");// THIS IS WHERE I PUT IT.

input = JOptionPane.showInputDialog(" To continue select 1 to play or select 0 to stop");
num =Integer.parseInt(input);

if ( num != 0){
System.out.println(" Game rolling");

} else{
System.out.println(" Game over " );
System.out.println("Here is your money:" + " " + money.format(amount));

}while( num !=1){

}
}
}
}
}

This post has been edited by kealh: 12 December 2007 - 10:01 PM

#4 cfoley

• Cabbage

Reputation: 2391
• Posts: 5,016
• Joined: 11-December 07

Re: if statement.

Posted 13 December 2007 - 09:26 AM

Your code is very hard to read because you have not indented it. This makes it difficult to match up the start { to the end }. }

It looks to me like you forgot to close the else block here:

``` } else{
amount = amount -2;
//	  <----- should there be a close brace here?

```

Did you get a compile error and just added a curly bracket to the end to fix it? When that happens you really need to look through your code to find out where it should be, otherwise the program won't work as you intended it.

If you fix the indentation, I'll have another look. However, at a first glance that's what I see. I'm not 100% sure how this forum is set up but you might have to place code tags around your code to keep the spacing and indentation. Do it like this:

[code]
public static void main( String[] args );
//more code
[/code ]

... but remove the space in the second tag.

#5 AbuJaFaR

• D.I.C Regular

Reputation: 13
• Posts: 330
• Joined: 13-December 07

Re: if statement.

Posted 13 December 2007 - 10:33 AM

cfoley, on 11 Dec, 2007 - 07:11 AM, said:

```// Set Up Game
while ( condition ) {
// Play Game
// Ask user if he wants to continue
} // while end

```

Cant he also use the do..while loop?
cause the user will play at least one time right?

#6 kealh

Reputation: 0
• Posts: 14
• Joined: 06-December 07

Re: if statement.

Posted 13 December 2007 - 03:58 PM

Even after i put the bracer where you said i was missing. I still cant get it to stop without it saying Game over and Game on because the player put in 1.

#7 cfoley

• Cabbage

Reputation: 2391
• Posts: 5,016
• Joined: 11-December 07

Re: if statement.

Posted 13 December 2007 - 06:03 PM

Post what you've got with the proper indentation.

AbuJaFaR, not necessarily. What if the user only puts in one credit, or enters a nonsense value like -5?

#8 AbuJaFaR

• D.I.C Regular

Reputation: 13
• Posts: 330
• Joined: 13-December 07

Re: if statement.

Posted 13 December 2007 - 06:17 PM

cfoley, on 13 Dec, 2007 - 06:03 PM, said:

AbuJaFaR, not necessarily. What if the user only puts in one credit, or enters a nonsense value like -5?

yeah indeed