我是java編程的新手,對於初學者問題非常抱歉。我很確定我忽略了一些簡單的東西,但是我在以前的主題中找不到解決方案。getter總是返回0
我想建立一個對象「computerDraw」。當我創建一個新的「computerDraw」時,我想讓計算機決定他是否從int數組中抽取1,2或3個數字。數字隨機抽出。
繪製的數字應該存儲在變量「computerSumCards」中。
我創建了一個getter來返回computerSumCards,但它每次都返回「0」。我想不出爲什麼。謝謝您的幫助!
public class computerDraw {
int computerSumCards;
int getAmountOfComputerCards;
int randomCard;
int draws = 0;
private int[] possibleDraws = {1, 2, 3};
int computerCanDraw = (possibleDraws[new Random().nextInt(possibleDraws.length)]);
public void drawLoop() {
while (draws < computerCanDraw) {
int[] cards = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
randomCard = (cards[new Random().nextInt(cards.length)]);
computerSumCards += randomCard;
}
}
public int getComputerSumCards() {
return computerSumCards;
}
public int getAmountOfComputerCards() {
getAmountOfComputerCards = draws;
return getAmountOfComputerCards;
}
}
你叫'drawLoop()'嗎? – Guy
@Guy:不,我沒有,但是我收到了一個錯誤,並發現(在另一個主題中),我無法直接在類體中循環設置。我仍然試圖弄清楚。這與這個問題有關嗎?我認爲還有一個不同的問題。 – BjornsBot
我也想知道爲什麼這個問題得到了downvotes。我事先做了研究,並試圖整合/重新編譯和執行。如果我在做/提出錯誤的問題,請解釋,以便在提出未來問題時避免它。謝謝。 – BjornsBot