【问题标题】:Java adding value to existing value (int)Java 将值添加到现有值 (int)
【发布时间】:2014-07-19 21:37:00
【问题描述】:

所以我正在制作这个彩票游戏来练习,我几乎完成了(我是 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();

}
}

【问题讨论】:

  • 但是每次按下按钮时,您都将银行声明为 0。您需要将范围扩大到一个实例变量(将其移到 actionPerformed 方法之外),这样会更好。

标签: java int double addition


【解决方案1】:

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

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

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

【讨论】:

  • @ErikPragt 也许它应该是一个答案而不是评论。 ;)
  • 哦,就这么简单,把它放在类本身,不要放在任何空隙中!我试图让它在我的 void 中未定义,但后来出现错误,所以我尝试通过输入 0 来修复它,这就是我遇到这个问题的原因,但现在它可以工作了。谢谢老哥!
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
相关资源
最近更新 更多