[英]How to change the value of a boolean from false to true using a button
我为我的 while 循环制作了一个 boolean 以使其在 boolean 等于true
时运行,我想让播放按钮使boolean = true
触发while循环并运行游戏。 但这由于某种原因不起作用。
有人可以帮助制作boolean gameRunning = true;
? 我只是不知道如何将其值从false
更改为true
。
我尝试使用atomic booleans
,但没有奏效
package panda.org;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.util.concurrent.atomic.AtomicBoolean;
public class NumberGame implements ActionListener{
JFrame frame;
JLabel rules;
JLabel rulesText;
JLabel rulesText2;
JButton play;
JButton exit;
Font myFont = new Font("Serif Plain", Font.BOLD, 15);
NumberGame() {
frame = new JFrame("NumberGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(true);
Image icon = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Gaming MSI\\Pictures\\Saved Pictures\\download (1).png");
frame.setIconImage(icon);
rules = new JLabel("Rules: ");
rules.setFont(myFont);
rules.setBounds(50, 100, 100, 75);
rulesText = new JLabel("We will pick a random number in the range of 1 -> 50.");
rulesText.setBounds(100, 100, 315, 75);
rulesText2 = new JLabel("Your job is to guess that number!");
rulesText2.setBounds(100, 120, 315, 75);
play = new JButton("Play");
play.setBounds(150, 300, 100, 75);
boolean gameRunning = false;
play.addActionListener(e -> {
gameRunning = true;
});
while(gameRunning = true) {
JLabel label = new JLabel("Guess the number from 1 till 50");
label.setFont(myFont);
label.setBounds(150, 75, 315, 75);
JLabel hints = new JLabel("");
hints.setBounds(150, 180, 1000, 100);
JLabel hints2 = new JLabel("");
hints2.setBounds(150, 200, 1000, 100);
JTextField text = new JTextField();
text.setBounds(250, 150, 100, 25);
JButton check = new JButton("Check");
check.setBounds(150, 150, 75, 25);
double randomDouble = Math.random();
randomDouble = randomDouble * 50 + 1;
double randomDouble2 = Math.random();
randomDouble2 = randomDouble2 * (15 - 5 + 1) + 5 ;
double randomDouble3 = Math.random();
randomDouble3 = randomDouble3 * (15 - 5 + 1) + 5 ;
int randomHint = (int) randomDouble2;
int randomHint2 = (int) randomDouble3;
int randomInt = (int) randomDouble;
System.out.println("nb: " + randomInt);
System.out.println("hint: " + randomHint);
System.out.println("hint2: " + randomHint2);
JLabel status = new JLabel("");
status.setBounds(150, 160, 1000, 100);
JLabel closeness = new JLabel("");
closeness.setBounds(150, 220, 1000, 100);
closeness.setForeground(Color.blue);
final int[] failedAttempts = {0};
check.addActionListener(e1 -> {
String nb = text.getText();
int change = Integer.parseInt(nb);
frame.add(status);
if (randomInt == change) {
status.setText("You chose the correct number!");
status.setForeground(Color.green);
hints.setText("");
hints2.setText("");
}
if (randomInt > change) {
closeness.setText("Your answer is smaller than the correct answer");
}
if (randomInt < change) {
closeness.setText("Your answer is larger than the correct answer");
}
if (randomInt != change) {
status.setText("Wrong choice! Try again.");
status.setForeground(Color.red);
failedAttempts[0]++;
if (failedAttempts[0] == 3) {
int plus = randomInt + randomHint;
int minus = randomInt - randomHint2;
hints.setText("Hint: I see you are struggling, here is a low range to make it easier!");
hints2.setText("The lowered range is from " + plus + " to " + minus);
}
}
});
rules.setText("");
rulesText.setText("");
rulesText2.setText("");
frame.add(hints);
frame.add(hints2);
frame.add(label);
frame.add(check);
frame.add(closeness);
frame.add(text);
}
exit = new JButton("Exit");
exit.setBounds(350, 300, 100, 75);
exit.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(frame,"Are you sure want to exit?", "Exit",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
System.exit(0);
}
});
frame.add(play);
frame.add(exit);
frame.add(rules);
frame.add(rulesText);
frame.add(rulesText2);
frame.setVisible(true);
}
public static void main(String[] args) {
NumberGame number = new NumberGame();
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
您仍在考虑控制台应用程序,Swing 旨在处理事件......
因此,考虑到上述情况,您不能期望您的代码在这里:
play.addActionListener(e -> {
gameRunning = true;
});
while(gameRunning = true) {
...
}
要按该顺序执行,因为您无法控制用户何时按下按钮,它可能在 2 秒内,也可能在 2 小时内
为此,您可能需要将while
循环移动到一个方法,当用户按下play
按钮时,您需要更改gameRunning = true
然后调用该其他方法,如下所示:
public void runGame() {
while(gameRunning) {
// Your code here
}
}
play.addActionListener(e -> {
if (!gameRunning) { //This validation is needed otherwise if you press the button multiple times you'll have multiple loops running
gameRunning = true;
runGame();
}
});
这样,在用户按下play
按钮之前,您不会开始游戏。
请注意我刚刚写了没有==
的while(gameRunning)
,正如上面的评论中提到的,如果你有gameRunning = true
你给它一个值而不是比较它,当使用boolean
变量时,你可以简单地这样写,它减少了像这样的错别字的可能性。
if(true)
与if (true == true)
相同if(!false)
与if (false == false)
相同if(false)
与if (true == false)
相同正如我在之前的回答中提到的那样,避免使用null-layout
和setBounds
,为什么要实现ActionListener
而从不使用它? 它是空的,所以只需删除implements ActionListener
您不能更改 lambda function 中的局部变量的值。 正如我确信编译器会告诉你的那样,它们需要是最终的或有效的最终,这意味着它们只被分配一次。
解决方案是使用可以容纳您的 Boolean 的类型并更改它所持有的值。
AtomicBoolean gameRunning = new AtomicBoolean(false);
play.addActionListener(e -> {
gameRunning.set(true);
});
...
while(gameRunning.get()) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.