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%确定问题是什么,错误信息是
Card.java:13:错误:&#39;;&#39;预期 public String toString(){
感谢任何帮助谢谢
答案 0 :(得分:3)
您的toString()
方法位于Card(int number, String suite)
构造函数中。把它移出来。
答案 1 :(得分:1)
String method
位于类卡的构造函数内部,将其移到外面以便您可以使用它,因此您可以使用有效的卡constr
。
答案 2 :(得分: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;
}
}
答案 3 :(得分: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
中,请不要忘记格式化