gpt4 book ai didi

java - 如何更正石头剪刀布 GUI 游戏的代码?

转载 作者:行者123 更新时间:2023-12-02 09:01:31 25 4
gpt4 key购买 nike

我对 Java 编程还很陌生,我想尝试做一个 GUI 应用程序,但现在我陷入了 if/else 语句机制。到目前为止,除了“Rock Bet”和“Paper Bet”机制之外,一切都在运行。我已经测试了 Scissor Bet,一切正常。

这里的问题是,每当计算机显示它选择并运行 if/else 语句时,结论总是错误的。 “石头赌”和“布赌”与剪刀赌有何不同?

(我尝试将“剪刀下注”if 语句机制放在顶部、石头和纸 if/else 语句之前,但不知何故,判决在那里变得错误。是我的订单安排错误吗?)

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class gameRPS extends JFrame {
private JPanel mainPanel;
private JLabel textLabel1;
private JButton rockButton;
private JButton paperButton;
private JButton scissorButton;
private JLabel textLabel2;
private JLabel computersLabel;
private JLabel userBet;
private JButton enterButton;
private JLabel verdictLabel;

public gameRPS(String title) {
super(title);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(mainPanel);
this.pack();

rockButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: ROCK!");
}
});
paperButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: PAPER!");
}
});
scissorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
userBet.setText("YOU PICKED: SCISSOR!");
}
});

String[] rpsChoices = {"ROCK!", "PAPER!", "SCISSOR!"};
Random rand = new Random();
int computerBet = rand.nextInt(rpsChoices.length);

enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {

String computersChoice = rpsChoices[computerBet];
String userBetRock = "ROCK!";
String userBetPaper = "PAPER!";
String userBetScissor = "SCISSOR!";

computersLabel.setText(computersChoice); //Displays the Computer's Choice

//Mechanics for the Rock Bet
if (userBetRock == "ROCK!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("DRAW");
}
else if (userBetRock == "ROCK!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("YOU LOSE");
}
else if (userBetRock == "ROCK!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("YOU WIN");
}
//Mechanics for the Paper Bet
if (userBetPaper == "PAPER!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("DRAW");
}
else if (userBetPaper == "PAPER!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("YOU WIN");
}
else if (userBetPaper == "PAPER!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("YOU LOSE");
}
//Mechanics for the Scissor Bet (WORKING)
if (userBetScissor == "SCISSOR!" && computersChoice.equals("SCISSOR!")) {
verdictLabel.setText("DRAW");
}
else if (userBetScissor == "SCISSOR!" && computersChoice.equals("ROCK!")) {
verdictLabel.setText("YOU LOSE");
}
else if (userBetScissor == "SCISSOR!" && computersChoice.equals("PAPER!")) {
verdictLabel.setText("YOU WIN");
}
}
});

}
public static void main(String[] args) {
JFrame frame = new gameRPS("Rock Paper Scissors!");
frame.setSize(300,320);
frame.setResizable(true);
frame.setVisible(true);

}
}

最佳答案

在 Java 中,您无法像此处那样使用 == 检查字符串是否相等:

if (userBetPaper == "PAPER!" && ...) ...

在所有其他 if 中,您必须按照 if 的第二个条件执行操作,使用 .equals():

if (userBetPaper.equals("PAPER!") && ...) ...

但是,例如,我认为您所做的事情也存在逻辑错误

String userBetRock = "ROCK!";
...
if (userBetRock == "ROCK!" &&...)...

始终为 true,您永远不会更改它,也永远不会使用用户输入:例如,在按钮的 ActionEvent 监听器上,您可以保存在 String 在你的类中他选择了什么,然后在 enterButton 操作监听器中检查 String 包含什么

关于java - 如何更正石头剪刀布 GUI 游戏的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126245/

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