0

我正在开发一个石头剪刀布gui应用程序,但我一直在想如何让它工作。我发现在第 49 行我得到一个非法的表达式开始错误。虽然我意识到这是一项任务,但我不确定是什么触发了错误:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Rps extends JFrame
 implements ActionListener
{
private final char moves[] = {'R', 'P', 'S'};
private JRadioButton rock, paper, scissors;
private JTextField display;

public Rps()
{
super("Rock, paper, Scissors");

rock = new JRadioButton("   Rock   ", true);
paper = new JRadioButton("   Paper  ");
scissors = new JRadioButton(" Scissors ");

ButtonGroup rpsButtons = new ButtonGroup();
rpsButtons.add(rock);
rpsButtons.add(paper);
rpsButtons.add(scissors);

JButton go = new JButton("         Go         ");
go.addActionListener(this);

display = new JTextField(25);
display.setEditable(false);
display.setBackground(Color.yellow);

Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(rock);
c.add(paper);
c.add(scissors);
c.add(go);
c.add(display);
}

/**
*  returns -1 if the player wins,
*  0 if it's a tie, and 1 if the computer wins
*/
private int nextPlay(char computerMove, char playerMove)
{
if (computerMove == 'R'&&playerMove == 'S'||computerMove=='S'&&playerMove==='P'||computerMove=='P'&&playerMove=='R'){ //Where I get the error
 int r = 1;
 return r;
 }
else if (computerMove == 'R'&&playerMove == 'R'||computerMove=='S'&&playerMove=='S'||computerMove=='P'&&playerMove=='P'){
 int r = 0;
 return r;
}
else if (computerMove == 'S'&&playerMove == 'R'||computerMove=='P'&&playerMove=='S'||computerMove=='R'&&playerMove=='P'){
 int r = -1;
 return r;
}   

}

public void actionPerformed(ActionEvent e)
{
  char playerMove, computerMove;
  if (rock.isSelected())
    playerMove = 'R';
  else if (paper.isSelected())
    playerMove = 'P';
  else if (scissors.isSelected())
    playerMove = 'S';

 int k = (int)(Math.random() * 3);
computerMove = moves[k];
int result = nextPlay(computerMove, playerMove);

String msg = "  You said " + makeWord(playerMove) + ", I said " +
             makeWord(computerMove);
if (result < 0)
  msg += " -- you win.";
else if (result == 0)
  msg += " -- tie.";
else if (result > 0)
  msg += " -- I win.";
display.setText(msg);
}

private String makeWord(char move)
{
  String word = "";

switch (move)
{
  case 'R': word = "ROCK"; break;
  case 'P': word = "PAPER"; break;
  case 'S': word = "SCISSORS"; break;
}
return word;

}

public static void main(String[] args)
{
  Rps window = new Rps();
  window.setBounds(300, 300, 300, 140);
  window.setDefaultCloseOperation(EXIT_ON_CLOSE);
  window.setVisible(true);
}

}

4

3 回答 3

3

您使用的是assignmentoperator= 您需要在您的块中使用comparisonoperator 。==if

if (computerMove == 'R'&& playerMove =='S'||computerMove=='S'&&playerMove=='P'|| computerMove=='P'&& playerMove=='R')

编辑:

有错别字

playerMove==='P'在你的if陈述中

于 2013-09-25T19:53:06.780 回答
0

尝试这个 :

 if ((computerMove == 'R'&&playerMove == 'S')||(computerMove=='S'&&playerMove=='P')||(computerMove=='P'&&playerMove=='R')){
     int r = 1;
     return r;
     }
else if ((computerMove == 'R'&&playerMove = 'R')||(computerMove=='S'&&playerMove=='S')||(computerMove=='P'&&playerMove=='P')){
 int r = 0;
 return r;
}
else if ((computerMove == 'S'&&playerMove == 'R')||(computerMove=='P'&&playerMove=='S')||(computerMove=='R'&&playerMove=='P')){
 int r = -1;
 return r;
于 2013-09-25T19:57:00.137 回答
0

除了其他人所说的使用 == 而不是 = 之外,我只会使用括号。不会伤害任何东西,并提高了代码的可读性。所以你的陈述看起来像:

if (
(computerMove == 'R'&&playerMove =='R') ||
(computerMove=='S'&&playerMove=='S') ||
(computerMove=='P'&&playerMove=='P')
)...
于 2013-09-25T19:57:18.030 回答