- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明:
学习目标:
描述:编写一个程序与计算机玩剪刀石头布
要求:
创建一个枚举来表示三种选择:石头、纸和剪刀。
创建一个与计算机玩石头剪刀布的 GUI 应用程序
*编辑
好吧,我已经到了最后冲刺阶段,大部分真正需要的是evaluate() 方法。关于如何去做有什么想法吗?过去一个小时我一直在绞尽脑汁,但还是想不出来。无论如何,这是更新后的代码:
import javax.swing.ImageIcon;
public enum RPSChoice {
ROCK(new ImageIcon(RPSChoice.class.getResource("rock.png"))),
PAPER(new ImageIcon(RPSChoice.class.getResource("paper.png"))),
SCISSORS(new ImageIcon(RPSChoice.class.getResource("scissors.png")));
private ImageIcon imgChoice;
private RPSChoice(ImageIcon imgChoice) {
this.imgChoice = imgChoice;
}
public ImageIcon getImageIcon(){
return imgChoice;
}
public static void evaluate(){
//TODO
}
public static RPSChoice randomChoice(){
return values()[(int) (Math.random() * values().length)];
}
}
还有图形用户界面
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.GridLayout;
public class RockPaperScissorsGUI extends JFrame {
private JPanel contentPane;
private JPanel humanDisplay = new JPanel();
private JPanel computerDisplay = new JPanel();
private JLabel winnerAnnouncement = new JLabel("Winner Shown Here");
private JPanel choicePanel = new JPanel();
private RPSChoice rock = RPSChoice.ROCK;
private RPSChoice paper = RPSChoice.PAPER;
private RPSChoice scissors = RPSChoice.SCISSORS;
private RPSChoice randChoice;
private final JPanel humOrCompPanel = new JPanel();
private final JLabel lblHuman = new JLabel("Human");
private final JLabel lblComputer = new JLabel("Computer");
private JButton btnScissors = new JButton("Scissors");
private JButton btnPaper = new JButton("Paper");
private JButton btnRock = new JButton("Rock");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RockPaperScissorsGUI frame = new RockPaperScissorsGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RockPaperScissorsGUI() {
setTitle("Rock, Paper, Scissors");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 544, 366);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel choicePanel = addPanels();
addButtons(choicePanel, rock, btnRock);
addButtons(choicePanel, paper, btnPaper);
addButtons(choicePanel, scissors, btnScissors);
}
private JPanel addPanels() {
contentPane.add(humanDisplay, BorderLayout.WEST);
humanDisplay.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
contentPane.add(computerDisplay, BorderLayout.EAST);
winnerAnnouncement.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(winnerAnnouncement, BorderLayout.CENTER);
contentPane.add(choicePanel, BorderLayout.NORTH);
contentPane.add(humOrCompPanel, BorderLayout.SOUTH);
humOrCompPanel.setLayout(new GridLayout(1, 0, 0, 0));
lblHuman.setHorizontalAlignment(SwingConstants.CENTER);
humOrCompPanel.add(lblHuman);
lblComputer.setHorizontalAlignment(SwingConstants.CENTER);
humOrCompPanel.add(lblComputer);
return choicePanel;
}
private void addButtons(JPanel choicePanel, RPSChoice choice, JButton button ){
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
humanDisplay.removeAll();
humanDisplay.add(new JLabel(choice.getImageIcon()));
computerDisplay.removeAll();
randChoice = RPSChoice.randomChoice();
computerDisplay.add(new JLabel(randChoice.getImageIcon()));
choicePanel.repaint();
choicePanel.revalidate();
}
});
choicePanel.add(button);
}
}
感谢大家迄今为止提供的所有帮助!
最佳答案
您可以将变量 ImageIcon 添加到枚举中,并将 ROCK、PAPER、SCISSORS 与其图像相关联:
public enum RPSChoice {
ROCK(new ImageIcon(RPSChoice.class.getResource("rock.png"))),
PAPER(new ImageIcon(RPSChoice.class.getResource("paper.png"))),
SCISSORS(new ImageIcon(RPSChoice.class.getResource("scissors.png")));
ImageIcon img;
private RPSChoice(ImageIcon img) {
this.img = img;
}
public ImageIcon getImage() {
return img;
}
}
关于Java:布、石头、剪刀 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662834/
显然是在学习java。我能够让游戏正常运行。但我需要这样做,以便如果用户放置 R/P/S 以外的东西,那么它默认为 Rock。我不需要循环。如果我放石头的话,游戏就完美了。如果我放置 RPS 以外的任
我刚刚开始使用 python,需要一些帮助!我当时在做剪刀石头布游戏,我想在人或计算机赢得 3 场胜利后添加一个重启选项。 我已经四处寻找一些答案,但从我看到的所有其他代码来看,似乎超出了我的范围,或
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我正在用 C# 制作石头、剪刀、布游戏,目前在有人输入非 R、S 或 P 的输入时尝试显示消息时遇到问题。例如,我正在尝试获取默认值在 switch 语句中工作,但我没有运气。这就是我目前所拥有的。如
我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明: 学习目标: 练习枚举的使用 创建一个包含构造函数、字段和方法的枚举 设计并实现您自己的 GUI 创建一个可运行的 jar 描述:编写一
我正在用Python创建一个石头剪刀布游戏。我的方法如下。然而,我不想打印玩家 1 和玩家 2,而是想让它们成为真实的名字。因此,可以说 John Wins 或 Joe Wins。如何实现每次打印玩家
我正在尝试完成类里面的石头、剪刀、布作业。 我收到“UnboundLocalError:赋值前引用的局部变量“绑定(bind)”” 错误。 有人可以告诉我为什么会收到此错误吗? import rand
我在 C++ 上学习石头剪刀布游戏时有一个游戏实例类。我想创建整数常量,它表示游戏中允许的符号数。对于经典的 rsp 游戏,它是 3(石头、剪刀和布),但是有一些有趣的 rcs 游戏扩展带有额外的符号
我正在上 Codecademy 类(class)(找到 here ),但一直告诉我“当输入是纸和石头时,您的代码返回‘石头获胜’而不是‘纸获胜’”,为什么?应该是正确的。既然它在谈论“石头获胜”,那么
我正在可汗学院编写一个剪刀石头布游戏,这样我就可以看到视觉效果,但是 var Compare = function(choice1, choice2) 无法正常工作。 html 它工作得很好。插入了我
对于我的类(class)项目,我们要制作一个“石头剪刀布”游戏,使用一个函数来显示菜单和验证输入,以及一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数
我目前受困于我的 RPS 程序,因为它无法正确存储用户丢失的次数或与计算机的关联。例如,当我运行程序并输入“q”退出时,我得到以下输出: Enter R, P, S, or Q (for quit)
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 7 年前。 此问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-topic在这里
我一直在尝试解决石头剪刀布项目,但我不知道如何做出 if/else 语句。我这样做了很多次,最后我认为我已经接近解决问题了,但问题是,每次运行程序时我都会得到错误的输出。 例如,我使用“Paper”,
以下是我在 BlueJ 中编写的石头剪刀布游戏的代码。当我编译并且用户输入输入时,计算机立即从playerWins()打印大量输出。当用户输入“退出”时游戏结束。有人可以帮助我,这样我的屏幕就不会被淹
如果之前曾问过此问题的某个版本,我们深表歉意。我查了一下,但找不到真正能解决我的问题的东西。 我今天开始学习Python,并尝试构建一个简单的石头剪刀布游戏。 我有以下代码,它运行得很好: impor
我今天一直在开发这个应用程序,但我似乎无法弄清楚代码有什么问题。 当我检查时,没有语法错误。但是当我尝试运行它并调试它时,它说 "Source not found" on "ActivityThrea
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我有 JS 中的基本“石头、剪刀、布”游戏的代码。它可以与提示一起使用,但我希望能够通过按钮做出选择。我想使用“getElementById”和“addEventListener(“click”)”。
有没有一种方法可以用大于号和小于号来做剪刀石头布游戏? 我知道执行 RPS 有不同的方法,但我想具体了解大于和小于。 这是我的代码: "Rock" > "Scissors" and 'Rock' "
我是一名优秀的程序员,十分优秀!