变量可能尚未在if / else if语句中初始化

时间:2013-09-25 20:37:39

标签: java compiler-errors variable-initialization

我已经发布了这段代码,但是有更多的问题,这次是变量初始化错误。第68行出现错误,变量为playerMove。我已经在互联网上搜索了为什么它抱怨该变量尚未初始化无济于事。谁能告诉我为什么我的IDE抱怨我没有为playerMove分配值?

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')){
 return 1;}
else if ((computerMove == 'R'&&playerMove == 'R')||  (computerMove=='S'&&playerMove=='S')||(computerMove=='P'&&playerMove=='P')){
 return 0;}
return -1;  
}

public void actionPerformed(ActionEvent e)
{
  char playerMove, computerMove; //Errors start around here
  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); //More errors

String msg = "  You said " + makeWord(playerMove) + ", I said " +
             makeWord(computerMove); //Even more errors
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);
}
}

1 个答案:

答案 0 :(得分:3)

您已声明playerMove,但在以下

char playerMove, computerMove; //Errors start around here
if (rock.isSelected()){
    playerMove = 'R';
} else if (paper.isSelected()){
    playerMove = 'P';
} else if (scissors.isSelected()){
    playerMove = 'S';
} // add an else with default value or initialize it in its declaration above 

如果if条件都没有评估为true会怎样?您的变量将保持非正常状态。编译器不允许这样做。向else添加if-else块或立即使用默认值初始化

char playerMove = '0';