2014-09-28 45 views
-2

我遇到了一些Java任務的麻煩。我不確定我在做什麼錯,但它告訴我,我需要一個標識符,並且當我嘗試add()ArrayList時,這些行是表達式的非法開始。Java - <identifier>必需/非法開始表達式

這裏是我的代碼,罪犯開始於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; 
    } 
} 

任何和所有幫助表示讚賞。

+0

其修改ArrayList中的線應該是在構造函數或其他一些方法。 – csmckelvey 2014-09-28 06:01:17

回答

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 */ 
}