Java - <identifier>必需/非法表达式开始</identifier>

时间:2014-09-28 05:56:43

标签: java debugging identifier

我在使用Java分配时遇到了麻烦。我不确定我做错了什么,但它告诉我我需要一个标识符,当我尝试add()时,这些行是表达式的非法开头ArrayList

这是我的代码,罪犯从printStrat.add("1. Tit-For-Tat\n");开始:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.lang.Math;

public class PDGame
{
  private static ArrayList<boolean> rcrd = new ArrayList();
  private static Gamestat globalStats = new Gamestat();
  private String[] rslt = new String[] {
     "You and your partner remain silent\n", 
     "Your partner testifies against you and you remain silent\n",
     "You testify against your partner and he remains silent\n",
     "You both testify against each other\n" };
  private ArrayList<String> printStrat = new ArrayList<String>();
  printStrat.add("1. Tit-For-Tat\n");
  printStrat.add("2. Tit-For-Two-Tats\n");
  printStrat.add("3. Tit-For-Tat with forgiveness\n"); 
  private int strat = 1;
  private int npcPrison = 0;
  private int pcPrison = 0;

  public PDGame() 
  {
    //there is no need for a scanner to read if I won't be doing
    // anything with it, so I did not implement it
  }

  public String playRound(int decision)
  {
    boolean myMove;
    if(strat == 1) { //Tit-for-Tat
      if(rcrd.size() < 1) //if first move
        myMove = false; 
      else //set npc move to last player's move
        myMove = rcrd.get(rcrd.size()-1);
    }
    else if(strat == 2) { //Tit-for-Two-Tats
      if(rcrd.size() < 2)  //if first move
        myMove = false; 
      else if(rcrd.get(rcrd.size()-1) == true && rcrd.get(rcrd.size()-2) == true)
        myMove = true; //if player betrayed last two times, betray
      else //otherwise, forgive
        myMove = false; 
    }
    else if(strat == 3) { 
      int ran = (int)(Math.random()*100+1);
      if(rcrd.size() < 1) //if first move,forgive
        myMove = false; 
      else if(ran < 50) //if ran > 75, act normally
        myMove = rcrd.get(rcrd.size()-1);
      else //if ran = 1-50, forgive
        myMove = false;
    }

    if(decision == 1) {
      if(myMove == false) {
        result = rslt[0];
        npcPrison = 2;
        pcPrison = 2;
      }
      else if(myMove == true) {
        result = rslt[1];
        npcPrison = 1;
        pcPrison = 5;
      }
      rcrd.add(false);
    }
    else if(decision == 2) {
      if(myMove == false) {
        result = rslt[2];
        npcPrison = 5;
        pcPrison = 1;
      }
      else {
        result = rslt[3];
        npcPrison = 3;
        pcPrison = 3;
      }
      rcrd.add(true);
    } 
    globalStats.update(pcPrison,npcPrison);
    globalStats.setDate();
    return result;
  }

  public ArrayList<String> getStrategies() 
  {
    return printStrat;
  }

  public void setStrategy(int strategy) 
  {
    strat = strategy;
    globalStats.setStrategy(strategy);
  }

  public GameStat getStats() 
  {
    return globalStats();
  }

  public String getScores() 
  {
    String scores = "Your prison sentence is: " + pcPrison + "\n";
    scores += "Your partner's prison sentence is " + npcPrison + "\n";
    return scores;
  }
}

感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

你不能直接在class声明下放置代码(除了赋值) - 它应该放在方法,构造函数或匿名块中。解决此问题的一种方法是将您的printStrat.add调用移至构造函数:

public class PDGame
{
  /* snipped */

  private ArrayList<String> printStrat = new ArrayList<String>();

  public PDGame() 
  {
    printStrat.add("1. Tit-For-Tat\n");
    printStrat.add("2. Tit-For-Two-Tats\n");
    printStrat.add("3. Tit-For-Tat with forgiveness\n"); 
  }

  /* snipped */
}