This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

Slot Machine  RSS feed

Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been given a piece of coursework to do using random numbers. I have decided to do a slot machine for it. I am having trouble and would like help so that the result either adds 2 pound on to there money if they win or takes one off if they lose. I also would like a loop so that when they have no money left they can not play.

//Slot machine Assignment

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Slot extends Applet implements ActionListener, AdjustmentListener {

private TextField nameField; //start of attributes
private String name="", result="";
private Button tester;
private int count = 0;
private Scrollbar slider;
private int sliderValue = 0;
private int die, die2, die3;
private int money; //end of attributes

public void init() { //start of init

nameField=new TextField ("",20);
add (nameField);

tester = new Button("Press here");

slider = new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 100);
}//end of init

public void actionPerformed(ActionEvent event) { //start of action performed
name = nameField.getText();
if (event.getSource()==tester){//start of if statement
die = (int)(Math.random() * 3)+1;
die2 = (int)(Math.random() * 3)+1;
die3 = (int)(Math.random() * 3)+1;
if ((die == die2)&&(die == die3))
result = "sorry you lose" ;

}//end of if statement

}//end of action performed

public void adjustmentValueChanged (AdjustmentEvent e) {// start of changed adjustment value
sliderValue =slider.getValue();
money = sliderValue;
}//end of changed adjustment value

public void paint(Graphics g){//start of paint

g.drawRect(100,170,40,40); // draw a green rectangle

g.drawRect(150,170,40,40); // draw a red rectangle

g.drawRect(200,170,40,40); // draw a yellow rectangle

g.setColor(; // set colour of text to black
g.drawString("Slot Machine Assignment",50,10);
g.drawString("Please input your name in the field below",40,40);
g.drawString("Hello " + name, 100, 130);
g.drawString("The amount you started with is �"+ sliderValue, 100, 150);
g.drawString("The amount you have now is �"+ money, 100, 270);
g.drawString("" + die, 120, 190);
g.drawString("" + die2, 170, 190);
g.drawString("" + die3, 220, 190);
g.drawString (result, 100, 250);


Any help you could give would be greatly appreciated
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you are doing real good, so I don't understand what you are having problems with. Adding two and subtracting one seems so simple, maybe I am missing something?

Also you may want to use code tags to make your code easier to read.

[ March 20, 2007: Message edited by: carl sjostrom ]
Posts: 64159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question twice.
lowercase baba
Posts: 12738
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not post the same question in more than one forum. You now have two people trying to help you, who may not be aware of what the other is doing. It wastes at least one persons time.
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!