编译错误"非法开始表达"

时间:2014-04-06 05:36:56

标签: java compiler-errors

我尝试编译我的Card课程以进行视频扑克任务。我收到了illegal start of expression编译器错误。我很确定它指出的两种方法都是正确编写的 - 它们只是简单的getter方法。所以它一定是我没注意到的东西。请帮忙。谢谢。

public class Card实施Comparable<Card> {

private int suit; 
private int value; 

public Card(int s, int v)
{ //constructor of an object Card
    s = suit;
    v = value;
    //make a card with suit s and value v
}

public int compareTo(Card c)
{
    if(this.value > c.value)
    { 
        return 1;
    }
    if(this.value < c.value)
    {
        return -1;
    }
}       

    // use this method to compare cards so they 
    // may be easily sorted



public String toString() //to tell the user what card/s they have
{
    //for printing?//
//  ArrayList<String> forPrint = new ArrayList<String>();
    getHand();
    for(int i = 0; i < 5; i++)
    {
        hand.get(i).getSuit();
        hand.get(i).getValue();
        if(s == 1)
        {
            if(v == 11)
            {
                return "Jack of Clubs";
            //  forPrint.add("Jack of Clubs");
            }
            if(v == 12)
            {
                return "Queen of Clubs";
            //  forPrint.add("Queen of Clubs"); 
            }
            if(v == 13)
            {
                return "King of Clubs";
            //  forPrint.add("King of Clubs");  
            }
            if(v == 1)
            {
                return "Ace of Clubs";
            //  forPrint.add("Ace of Clubs")    
            }
            else
            {
                return v + " of Clubs";
            //  forPrint.add(v + " of Clubs");
            }   
        }
        if(s == 2)
        {
            if(v == 11)
            {
                return "Jack of Diamonds";
            //  forPrint.add("Jack of Diamonds");   
            }
            if(v == 12)
            {
                return "Queen of Diamonds";
            //  forPrint.add("Queen of Diamonds");  
            }
            if(v == 13)
            {
                return "King of Diamonds";
            //  forPrint.add("KIng of Diamonds");   
            }
            if(v == 1)
            {
                return "Ace of Diamonds";
            //  forPrint.add("Ace of Diamonds");    
            }
            else{
                return v + "of Diamonds";
            //  forPrint.add(v + " of Diamonds")    
        }
        if(s == 3)
        {
            if(v == 11)
            {
                return "Jack of Hearts";
            }
            if(v == 12)
            {
                return "Queen of Hearts";
            }
            if(v == 13)
            {
                return "King of Hearts";
            }
            if(v == 1)
            {
                return "Ace of Hearts";
            }
            else
            {
                return v + "of Hearts";
            }
        }
        if(s == 4)
        {
            if(v == 11)
            {
                return "Jack of Spades";
            }
            if(v == 12)
            {
                return "Queen of Spades";
            }
            if(v == 13)
            {
                return "King of Spades";
            }
            if(v == 1)
            {
                return "Ace of Spades";
            }
            else
            {
                return v + "of Spades";
            }
        }

    }
}
//DB method to set 1, 2, 3, and 4 to card suits


//now here create string representation for the Card    
    // use this method to easily print a Card object

public int getSuit()
{
    return s;
}
public int getValue()
{
    return v;
}   

//DB right now have cards in theDeck like Card(2, 10), need Card(d, 10)
//need to convert that to a String d10 

// add some more methods here if needed

}

这是我得到的编译错误:

$ javac Card.java
Card.java:149: illegal start of expression
    public int getSuit()
    ^
Card.java:149: ';' expected
    public int getSuit()
                      ^
Card.java:153: illegal start of expression
    public int getValue()
    ^
Card.java:153: ';' expected
    public int getValue()
                       ^
Card.java:163: reached end of file while parsing
}
 ^
5 errors

1 个答案:

答案 0 :(得分:2)

首先,

缺少}

    else {
       return v + "of Diamonds";
       ...