我应该制作一个石头、床单和剪刀游戏的程序。它涉及来自 Math.random() 的人类游戏和计算机游戏。它实际上正在工作,但只要我喜欢玩,我就必须一次又一次地重新运行它。所以我决定让它无限,直到输入 4。
这是它的工作原理:
想玩吗?击败电脑!
- 石头
- 工作表
- 剪刀
- 退出
意思是说,当我输入 4 时,它将退出,但只要输入 1、2 或 3,它就应该连续运行。我怎样才能做到这一点。这是我当前的代码。我知道错误是 while player != 4 的放置
import javax.swing.*;
public class StoneSheetShears
{
public static void main(String[] args)
{
String consoleStr;
int player = 0;
int[] numChoices = {1,2,3,4};
String[] strChoices = {"Stone", "Sheet", "Shears", "Quit"};
String playerChoice = "";
String compChoice = "";
int computer = (int)(Math.random() * 3);
String output = "";
while(true)
{
do
{
try
{
consoleStr = JOptionPane.showInputDialog("Beat the computer\n1. Rock\n2. Paper" +
"\n3. Scissors\n4. Quit ");
player = Integer.parseInt(consoleStr);
for(int x = 0; x <numChoices.length; x++)
{
if(player == numChoices[x])
{
playerChoice = strChoices[x];
}
}
for(int y = 0; y <numChoices.length; y++)
{
if(computer == numChoices[y])
{
compChoice = strChoices[y];
}
}
}
}while(player!=4)
catch (NumberFormatException err)
{
JOptionPane.showMessageDialog(null, "There is an error on entry",
"Error Message", JOptionPane.WARNING_MESSAGE);
continue;
}
break;
}
if (player == computer)
{
output = "Both are " + compChoice;
JOptionPane.showMessageDialog(null, output, "DRAW!", JOptionPane.INFORMATION_MESSAGE);
}
else if (player == 1)
{
if (computer == 2)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 3)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
else if (player == 2)
{
if (computer == 3)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 1)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
else if (player == 3)
{
if (computer == 1)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
}
else if (computer == 2)
{
output = "Computer move is " + compChoice +
"\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null,output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
这基本上是一个选择石板和剪刀的游戏。
- 人类玩家将从 1(石头)、2(薄片)、3(剪刀)、4(退出)中选择
- 电脑玩家是基于数学随机的。
我使用数组来定义人类玩家和计算机的 Action 。(数组是 Java 语法的一部分,不适合这样的描述。) - 如果双方的 Action 相同,则为平局。
- 如果人类选择布而计算机玩家选择剪刀,显然计算机获胜。这同样适用于其他选择。
玩家将输入另一个数字 1 2 或 3。(这只是重复步骤 1,因此没有必要)。当输入4时游戏将停止,即按照提示退出
我唯一的问题是如何让它持续工作直到输入 4。
最佳答案
这个循环
do
{
// Your choices...
}while (player != 4);
仅当用户选择选项4
时才会退出。您的游戏逻辑应包含在该循环中(这意味着您可能会丢失 while(true)
循环)。
这也是向用户呈现此类应用程序的一种糟糕方式。 Swing 是一个事件驱动的环境,这意味着您向用户呈现 UI 并对其交互使用react。从代码的外观来看,您已经采用了基本的命令行程序并尝试进行转换。这绝不是一个好主意。
图形用户界面应该很少包含像这样控制流程的循环。
更好的选择(恕我直言)是创建您自己的框架/对话框并向用户呈现选项。每个选项都会触发一系列事件,通过这些事件,游戏要么开始,要么终止。
这可以防止用户受到多个弹出窗口的轰炸,这很烦人。
(这只是一种可能的界面设计)
public class RockPaperScissors {
public static void main(String[] args) {
new RockPaperScissors();
}
public RockPaperScissors() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new GamePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GamePane extends JPanel {
public static final int STONE = 0;
public static final int SHEET = 1;
public static final int SHEARS = 2;
private JButton stoneButton;
private JButton sheetButton;
private JButton shearsButton;
private JButton quitButton;
private JLabel player;
private JLabel computer;
private JLabel winner;
public GamePane() {
winner = new JLabel("Come play", JLabel.CENTER);
player = new JLabel("Player", JLabel.CENTER);
computer = new JLabel("computer", JLabel.CENTER);
stoneButton = new JButton(new GameAction("Stone", STONE));
sheetButton = new JButton(new GameAction("Sheet", SHEET));
shearsButton = new JButton(new GameAction("Shear", SHEARS));
quitButton = new JButton("Quit");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(winner, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridy++;
gbc.gridwidth = 2;
add(player, gbc);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
add(computer, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
gbc.gridwidth = 1;
gbc.gridx = 0;
add(stoneButton, gbc);
gbc.gridx++;
add(sheetButton, gbc);
gbc.gridx++;
add(shearsButton, gbc);
gbc.gridx++;
add(quitButton, gbc);
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
protected void play(int action) {
int ai = (int)Math.round((Math.random() * 2));
updateChoice("Computer", computer, ai);
updateChoice("Player", player, action);
if (ai == action) {
winner.setText("Draw!");
} else if (action == STONE) {
if (ai == SHEET) {
winner.setText("Computer Wins");
} else if (ai == SHEARS) {
winner.setText("Player Wins");
}
} else if (action == SHEET) {
if (ai == STONE) {
winner.setText("Player Wins");
} else if (ai == SHEARS) {
winner.setText("Computer Wins");
}
} else if (action == SHEARS) {
if (ai == STONE) {
winner.setText("Computer Wins");
} else if (ai == SHEET) {
winner.setText("Player Wins");
}
}
}
protected void updateChoice(String prefix, JLabel label, int action) {
switch (action) {
case STONE:
label.setText(prefix + " chose STONE");
break;
case SHEET:
label.setText(prefix + " chose SHEET");
break;
case SHEARS:
label.setText(prefix + " chose SHEARS");
break;
}
}
public class GameAction extends AbstractAction {
private int action;
public GameAction(String text, int action) {
this.action = action;
putValue(NAME, text);
}
@Override
public void actionPerformed(ActionEvent e) {
play(action);
}
}
}
}
更新
正如答案第一部分所述:
你不需要两个循环。内部循环会阻止外部循环运行,直到用户选择选项 4
。
您的游戏逻辑属于 do-while
循环。
public class StoneSheetShears {
public static void main(String[] args) {
String consoleStr;
int player = 0;
int[] numChoices = {1, 2, 3, 4};
String[] strChoices = {"Stone", "Sheet", "Shears", "Quit"};
String playerChoice = "";
String compChoice = "";
int computer = -1; //(int) (Math.random() * 3);
String output = "";
do {
try {
consoleStr = JOptionPane.showInputDialog("Beat the computer\n1. Rock\n2. Paper"
+ "\n3. Scissors\n4. Quit ");
player = Integer.parseInt(consoleStr);
for (int x = 0; x < numChoices.length; x++) {
if (player == numChoices[x]) {
playerChoice = strChoices[x];
}
}
computer = (int) Math.round(Math.random() * 3);
compChoice = strChoices[computer];
if (player == computer) {
output = "Both are " + compChoice;
JOptionPane.showMessageDialog(null, output, "DRAW!", JOptionPane.INFORMATION_MESSAGE);
} else if (player == 1) {
if (computer == 2) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
} else if (computer == 3) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
} else if (player == 2) {
if (computer == 3) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
} else if (computer == 1) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
} else if (player == 3) {
if (computer == 1) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Lose!",
JOptionPane.INFORMATION_MESSAGE);
} else if (computer == 2) {
output = "Computer move is " + compChoice
+ "\nYour move is " + playerChoice;
JOptionPane.showMessageDialog(null, output, "You Win!",
JOptionPane.INFORMATION_MESSAGE);
}
}
} catch (NumberFormatException exp) {
JOptionPane.showMessageDialog(null, "Bad choice", "Bad Choice", JOptionPane.ERROR_MESSAGE);
}
} while (player != 4);
}
}
关于java - 使用数组的石材剪程序 Joption,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13924826/