2014-11-14 54 views
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]; 
     ^---^ 

但后来您要访问:

+1

您所显示的代码中没有“卡”变量。 – brycem 2014-11-14 23:54:44

+0

这是什么语言? C#? – Christian 2014-11-14 23:56:42

+0

这可能会有助于发布完整的代码,因为你已经显示没有意义。 – gerrod 2014-11-15 00:05:30

回答

1

为您定义您的卡阵列

top = card[0]; 

我不知道应该什么卡可以,但我相信你想访问cards而不是card

相关问题