gpt4 book ai didi

Java 将值添加到现有值(int)

转载 作者:行者123 更新时间:2023-11-30 11:16:29 24 4
gpt4 key购买 nike

所以我正在制作这个彩票游戏作为练习,我快完成了(我是 Java 的初学者,需要很多帮助才能做到这一点)。在完成之前我需要做的最后一件事是找到一种方法让我的“银行”在每次玩游戏时都不会重置。

该游戏是一种彩票,每次您按下一个按钮,您就会赢或输,并且该金额应该被添加到您的银行或从您的银行中减去。但它只是简单地替换了银行中已有的东西。这是整个游戏的代码,这也是它的外观图片:

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();

}
}

最佳答案

bank 变量的范围是 actionPerformed 方法。每次运行此方法时,它都会设置为 0

如果您希望在调用之间保留该值,请将其设为成员变量,如 frame

顺便说一句,我怀疑您不需要两个 JFrames。通常一个就够了。

关于Java 将值添加到现有值(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24845209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com