本文介绍了如何在对象中创建toString方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
}
}
除了内部的toString方法外,一切正常卡类。不是100%确定问题是什么,错误信息是
Everything works except the toString method within the Card class. Not 100% sure what the problem is, error message is
Card.java:13:错误:';'预期
public String toString(){
Card.java:13: error: ';' expected public String toString() {
感谢任何帮助谢谢
推荐答案
您的 toString()
方法在你的卡(int number,String suite)
构造函数中。将它移出。
Your toString()
method is inside your Card(int number, String suite)
constructor. Move it out.
这篇关于如何在对象中创建toString方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!