这可能是一件非常简单的事情,它将使我完全烦恼,但是请记住我被困住了。
我快要完成一项作业了,但是由于系统无法在此类(称为Pack类)中找到任何符号,我快要死了。我确实有一个称为PlayingCard类的单独的类,它很完整,但是现在我需要一个带有单独的测试器的类来完成分配。
这是代码,然后我将解释我要使用的代码。
import java.util.Random;
public class Pack
{
PlayingCard[] cards = new PlayingCard[52];
public Pack()
{
// Setting up array
String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};
// Intialising array
Pack() // Error 1
{
for (int i = 0; i < PlayingCard.length(); i++) // Error 2
{
PlayingCard[i] = i; // Error 3
}
}
}
public void shuffle()
{
for (int i = 0; i < 52; i++)
{
Random random = new Random();
int rand = random.nextInt(50)+1;
PlayingCard[i] = rand; // Error 3
}
}
@Override
public String toString()
{
}
}
这非常类似于纸牌游戏类。我有一个称为Pack的方法,在该方法中,我需要构造一个由52张卡片组成的Pack,按不同的西装和等级排序。混洗类很明显,它需要能够对已构建的甲板进行混洗。我已经采取了一系列的扑克牌阵列。虽然PlayingCard[] cards = new PlayingCard[52]
专门插入到分配模板中。
现在的问题是,即使PlayingCard是同一包中单独类的一部分,并且Pack已经是该类的一部分,NetBeans系统也无法找到Pack,PlayingCard和length()的符号。
那么,我在这里出错了吗?
编辑:我得到的错误是这样的:
cannot find symbol
symbol: method Pack()
location: class Pack
; expected
cannot find symbol
symbol: method length()
location: class PlayingCard()
cannot find symbol
location: variable PlayingCard
location: class Pack
这些将按顺序在上面的代码中表示为1、2、3。
编辑2:代码顶部的PlayingCard可能是构造函数(我使用的模板是为分配代码而设置的),请参阅新链接。
public class Pack
{
PlayingCard[] cards;
public Pack()
{
this.cards = new PlayingCard[52];
// Setting up array
String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};
// Intialising array
Pack()
{
for (int i = 0; i < PlayingCard.length(); i++)
{
PlayingCard[i] = i;
}
}
}
我仍然在第一段代码中看到错误。
我猜这些是错误:
错误3:PlayingCard是类而不是数组。卡是数组,变化
/*change this PlayingCard[i] = i */
/*to*/ cards[i] = i;
错误2:长度不是函数而是变量。
/*change this PlayingCard.length(); */
/*to*/ cards.length;
再次获得纸牌的长度(您的阵列称为纸牌,而不是PlayingCard)
错误1:为什么在函数内部有一个函数?您想在这里做什么?
尝试以下代码:-
import java.util.Random;
public class Pack
{
PlayingCard[] cards = new PlayingCard[52];
public Pack()
{
// Setting up array
String[] suits = {"SPADES", "CLUBS", "HEARTS", "DIAMONDS"};
String[] ranks = {"TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"};
// Intialising array
for (int i = 0; i < cards.length; i++)
{
cards[i] = i;
}
}
public void shuffle()
{
for (int i = 0; i < 52; i++)
{
Random random = new Random();
int rand = random.nextInt(50)+1;
cards[i] = rand; // Error 3
}
}
@Override
public String toString()
{
}
}
PS:您的代码中有几个问题。我建议您在这里遵循一些教程-http: //codingbat.com/,以很好地理解语言基础。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句