2016-02-10 44 views
0
import java.util.ArrayList; 

public class Card { 

    int number; 
    String suit; 

    public Card(int number, String suit) { 
    this.number = number; 
    this.suit = suit; 

     @Override 
     public String toString() { 

      String[] high = { 
      "Jack", 
      "Queen", 
      "King" 
      }; 

      String type; 

      if (number < 10) { 
       return String.valueOf(this.number) + " of " + this.suit; 
      } 
      else { 
       return high[this.number-10] + " of " + this.suit; 
      } 

      //return suit + " of " + type; 

      //return String.valueOf(number) + " of " + suit; 
     } 
    } 

    public static void main(String[] args) { 

    String[] suit = { 
     "Clubs", 
     "Diamonds", 
     "Spades", 
     "Hearts" 
    }; 

    // String[] high = { 
    // "Jack", 
    // "Queen", 
    // "King" 
    // }; 

    ArrayList<Card> deckOfCards = new ArrayList<Card>(52); 

    for (int j = 0; j < 4; j++) { 
     for (int i = 0; i < 13; i++) { 

     deckOfCards.add(new Card (i+1, suit[j])); 
      currentCard.toString(); 
     } 
    } 



    // @Override 
    // public String toString(Card card) { 
    // 
    // this.suit = suit; 
    // this.number = number; 
    // 
    // String type; 
    // 
    // if (number < 10) { 
    //  type = Integer.toString(number); 
    // } 
    // else { 
    //  type = high[i-number]; 
    // } 
    // 
    // return suit + " of " + type; 
    // } 

    // currentCard.toString(); 

    } 
} 

除了Card類中的toString方法之外,一切都可以使用。不是100%確定問題是什麼,錯誤信息是如何在對象內創建toString方法?

Card.java:13:error:';'預計 公共字符串的ToString(){

任何幫助表示讚賞感謝您

+0

你的構造包含您的toString方法。移動這個方法,一切都會好的。如果您不確定結構,請參閱:https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html –

+0

如果其中一個答案對您有幫助,請接受它和/或給予好評。 – nlloyd

+0

請使用像eclipse這樣的基本IDE,並利用'ctrl + 1'和'ctrl + space'功能,他們可以很快解決您的大部分問題。 – HopefullyHelpful

回答

3

toString()方法是你Card(int number, String suite)構造函數中。把它移出來。

1

String method是類卡的構造函數的內部,將它移到外面以便您可以使用它,因此您可以擁有有效的卡片constr

0

將toString放置在構造函數外部。

public Card(int number, String suit) { 
    this.number = number; 
    this.suit = suit; 
} 
@Override 
public String toString() { 

      String[] high = { 
      "Jack", 
      "Queen", 
      "King" 
      }; 

      String type; 

      if (number < 10) { 
       return String.valueOf(this.number) + " of " + this.suit; 
      } 
      else { 
       return high[this.number-10] + " of " + this.suit; 
      } 
    } 
0

您的toString()方法在您的構造函數中;

這裏是更正後的代碼

public class Card { 

    int number; 
    String suit; 

    public Card(int number, String suit) { 
    this.number = number; 
    this.suit = suit; 
    } 


     @Override 
     public String toString() { 

      String[] high = { 
      "Jack", 
      "Queen", 
      "King" 
      }; 

      String type; 

      if (number < 10) { 
       return String.valueOf(this.number) + " of " + this.suit; 
      } 
      else { 
       return high[this.number-10] + " of " + this.suit; 
      } 

      //return suit + " of " + type; 

      //return String.valueOf(number) + " of " + suit; 
     } 

// your main starts from here.. 

不要忘記在Eclipse中按Ctrl + Shift + F,如果您選擇格式化

相關問題