2014-11-09 47 views
-1

所以我模擬二十一點,在我的班級甲板上的遊戲,我的方法之一是對象爲String

public String toString(){ 
} 

其目的是返回與52線串和每行應該有一個卡片從上到下的描述。我已經創建了包含52個卡片對象的數組,即{心1,心2,...} 但我不知道如何開始這部分。有人能幫我這個部分嗎?

我在想,如果我可以創建以下文件:

public String toString(){ 
String myCards= myCards+card // supposed to add the string card to its already existing string 
String card; 
    for(int i=0; i<52; i++){ 

然後到這裏每個索引訪問對象和將其轉換成字符串卡。然後每當循環結束時,一個新的字符串將被添加到字符串我的卡,直到獲得所有的卡。但是,我不確定如何做到這一點。任何人都可以幫我開始這個,請問?

非常感謝!我着手它的工作。我修改了一些東西,而且工作。再次!

+1

添加語言標籤,更重要的是,您的努力。 – 2014-11-09 03:31:25

回答

-2

你可能會想給你的卡類,返回該卡的詳細信息,併爲您的甲板類toString方法,用一個for循環是這樣的:

string outStr=""; 
static String newline = System.getProperty("line.separator"); 
for (int i=1; i<cardArray.size(); i++){ 
    outStr+=newline+cardArray[i].toString(); 
} 
return outStr; 

我可能不會得到的Java語法很對,但這應該是一個好的開始。

相關問題