Reputation: 37
So I'm making this lottery game for practice and I'm almost done (I'm a beginner of java, needed alot of help to get this far). The final thing that I need before it's finished is to find a way for my "bank" to not reset each time the game is played.
The game is a lottery, each time you press a button you win or lose and that amount is supposed to be added or subtracted to or from your bank. But instead it simply replaces what is already in the bank. This is the code for the entire game and here is also a picture of what it looks like:
http://i.gyazo.com/6390524f6cdc7ddfdacb033499de4094.png
TheLottery.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
public class TheLottery extends JFrame implements ActionListener {
/**
**author Samy
*/
JFrame frame = new JFrame("The Lottery");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel southPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel northPanel = new JPanel();
JButton tip = new JButton("Tip");
JButton play = new JButton("Play");
JButton exit = new JButton("Exit");
JLabel label = new JLabel();
JLabel tableLabel = new JLabel();
private static final long serialVersionUID = 1L;
public void TheLottery() {
southPanel.add(tableLabel);
northPanel.add(tip);
northPanel.add(play);
northPanel.add(exit);
centerPanel.add(label);
mainPanel.add(southPanel, BorderLayout.SOUTH);
mainPanel.add(northPanel, BorderLayout.NORTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
tableLabel.setText("<html><center><font size=5 color='white'>The Realistic Lottery<br><br></font><font size=4 color='white'>Ticket Price: $25<br><br></font><font color='white'>300,000 to 1,000,000 - Loss<br>200,000 to 300,000 - $25<br>30,000 to 200,000 - $50<br>30,000 to 10,000 - $100<br>10,000 to 5,000 - $500<br>1000 to 5,000 - $1,000<br>100 to 1000 - $10,000<br>10 to 100 - $50,000<br>10 to 1 - $250,000<br> Less than 1 - $1,000,000</font><center></html>");
int width = 720;
int height = width/16*9;
frame.setSize(width,height);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.add(mainPanel);
southPanel.setBackground(new Color(0x222222));
centerPanel.setBackground(new Color(0x222222));
northPanel.setBackground(new Color(0x222222));
mainPanel.setBackground(new Color(0x222222));
mainPanel.setBorder(new EtchedBorder(new Color(0xAAAAAA),new Color(0x666666)));
play.addActionListener(this);
exit.addActionListener(this);
tip.setToolTipText("If you win big early, stop and never come back!");
play.setToolTipText("Click me to play the lottery!");
exit.setToolTipText("Click me to exit.");
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object action = e.getSource();
int bank = 0;
String win = null;
double lotteryChance = Math.random()*1000000;
if(action == play) {
if (lotteryChance > 300000) {
win = ("You lost! Better luck next time!");
bank -= 25;
} else if (lotteryChance < 300000 && lotteryChance > 200000) {
win = ("You've won $25!");
bank += 25;
} else if (lotteryChance < 200000 && lotteryChance > 30000) {
win = ("You've won $50!");
bank += 50;
} else if (lotteryChance < 30000 && lotteryChance > 10000) {
win = ("You've won $100!");
bank += 100;
} else if (lotteryChance < 10000 && lotteryChance > 5000) {
win = ("You've won $500!");
bank += 500;
} else if (lotteryChance < 5000 && lotteryChance > 1000) {
win = ("You've won $1,000!");
bank += 1000;
} else if (lotteryChance < 1000 && lotteryChance > 100) {
win = ("You've won $10,000!");
bank += 10000;
} else if (lotteryChance < 100 && lotteryChance > 10) {
win = ("You've won $50,000!");
bank += 50000;
} else if (lotteryChance < 10 && lotteryChance > 1) {
win = ("You've won $250,000!");
bank += 250000;
} else if (lotteryChance < 1 && lotteryChance > 0) {
win = ("YOU'VE WON THE JACKPOT OF $1,000,000!");
bank += 1000000;
} else {
win = ("Something went very wrong, the game has been reset.");
bank = 0;
}
System.out.println("Your number is: "+lotteryChance);
System.out.println(win);
}
if(action == exit) {
System.exit(1);
}
label.setText("<html><center><font color='white' size=5>Bank: $"+bank+"<br></font><font color='white'>Your number is: "+lotteryChance+"<br><br>"+win+"</font></center> </html>");
}
public static void main(String[] args) {
TheLottery n = new TheLottery();
n.TheLottery();
}
}
Upvotes: 2
Views: 2096
Reputation: 533880
The scope of the bank
variable is the actionPerformed
method. Every time you run this method it is set to 0
.
If you want the value to be kept between calls, make it a member variable like frame
is.
BTW I suspect you don't need two JFrames. Usually one is enough.
Upvotes: 1