0
當我打電話的卡陣列中的我的洗牌方法中使用,它就像一個魅力:爲什麼我的一些方法可以訪問創建的數組,但其他人不能? (JAVA)
Card [ ] cards = new Card[52]; //I create my array here, outside of any method
Random rnGen = new Random();
public Deck() //Here is where I declare all the values for my array elements
{
cards[0] = new Card(11,"ACE","CLUB");
cards[1] = new Card(2,"TWO","CLUB");
cards[2] = new Card(3, "3", "CLUB");
cards[3] = new Card(4, "4", "CLUB");
cards[4] = new Card(5, "5", "CLUB");
... //52 statements declaring every single card.
}
public void shuffle() //This method is able to draw
{ //from the array with no problems
Card temp = new Card();
for(int k = 0; k < 7000; k++;
{
f = rnGen.nextInt(52);
s = rnGen.nextInt(52);
temp = cards[f]; //Calling in elements from array
cards[f] = cards[s]; //and it works
cards[s] = temp;
}
}
但隨後問題就出現了,當我嘗試從陣列中的頂級元素調用:
public Card getTopCard()
{
Card top = new Card();
top = card[0]; //This is the line that has the error
return top;
}
的錯誤狀態:「需要數組,但卡上找到」
爲什麼不能我的iPod Shuffle()方法來訪問我的數組沒有問題的,但我的topcard()方法不能? 我沒有做過任何不同的事情,我的數組仍然在類的另一部分中聲明。
如果你能啓發我爲什麼會出現這種情況,我會非常感激,因爲我實際上想知道爲什麼這是一個錯誤。
Card [ ] cards = new Card[52];
^---^
但後來您要訪問:
您所顯示的代碼中沒有「卡」變量。 – brycem 2014-11-14 23:54:44
這是什麼語言? C#? – Christian 2014-11-14 23:56:42
這可能會有助於發佈完整的代碼,因爲你已經顯示沒有意義。 – gerrod 2014-11-15 00:05:30