Java 数组方法引发类预期错误
不确定出现此错误的原因: 班级期望 扑克牌[].shuffleDeck() 错误:';'预期 扑克牌[].shuffleDeck() 守则: 公共班机{Java 数组方法引发类预期错误,java,arrays,class,methods,invoke,Java,Arrays,Class,Methods,Invoke,不确定出现此错误的原因: 班级期望 扑克牌[].shuffleDeck() 错误:';'预期 扑克牌[].shuffleDeck() 守则: 公共班机{ int index; Card [] deck; public void shuffleDeck(){ for (int i = 0; i<deck.length; i++){ index = (int)(Math.random()*deck.length);
int index;
Card [] deck;
public void shuffleDeck(){
for (int i = 0; i<deck.length; i++){
index = (int)(Math.random()*deck.length);
deck[index] = deck[i];
}
}
public String dealCard(){
String dealer = deck[0] + " ";
return dealer;
}
public static void main(String[] args) {
Card card0 = new Card();
Card card1 = new Card();
Card card2 = new Card();
Card card3 = new Card();
Card card4 = new Card();
Card card5 = new Card();
Card card6 = new Card();
Card card7 = new Card();
Card card8 = new Card();
Card card9 = new Card();
Card card10 = new Card();
Card card11 = new Card();
Card card12 = new Card();
Card card13 = new Card();
Card card14 = new Card();
Card card15 = new Card();
Card card16 = new Card();
Card card17 = new Card();
Card card18 = new Card();
Card card19 = new Card();
Card card20 = new Card();
Card card21 = new Card();
Card card22 = new Card();
Card card23 = new Card();
Card card24 = new Card();
Card card25 = new Card();
Card card26 = new Card();
Card card27 = new Card();
Card card28 = new Card();
Card card29 = new Card();
Card card30 = new Card();
Card card31 = new Card();
Card card32 = new Card();
Card card33 = new Card();
Card card34 = new Card();
Card card35 = new Card();
Card card36 = new Card();
Card card37 = new Card();
Card card38 = new Card();
Card card39 = new Card();
Card card40 = new Card();
Card card41 = new Card();
Card card42 = new Card();
Card card43 = new Card();
Card card44 = new Card();
Card card45 = new Card();
Card card46 = new Card();
Card card47 = new Card();
Card card48 = new Card();
Card card49 = new Card();
Card card50 = new Card();
Card card51 = new Card();
card0.setCard(1, 1);
card1.setCard(2, 1);
card2.setCard(3, 1);
card3.setCard(4, 1);
card4.setCard(5, 1);
card5.setCard(6, 1);
card6.setCard(7, 1);
card7.setCard(8, 1);
card8.setCard(9, 1);
card9.setCard(10, 1);
card10.setCard(11, 1);
card11.setCard(12, 1);
card12.setCard(13, 1);
card13.setCard(1, 2);
card14.setCard(2, 2);
card15.setCard(3, 2);
card16.setCard(4, 2);
card17.setCard(5, 2);
card18.setCard(6, 2);
card19.setCard(7, 2);
card20.setCard(8, 2);
card21.setCard(9, 2);
card22.setCard(10, 2);
card23.setCard(11, 2);
card24.setCard(12, 2);
card25.setCard(13, 2);
card26.setCard(1, 3);
card27.setCard(2, 3);
card28.setCard(3, 3);
card29.setCard(4, 3);
card30.setCard(5, 3);
card31.setCard(6, 3);
card32.setCard(7, 3);
card33.setCard(8, 3);
card34.setCard(9, 3);
card35.setCard(10, 3);
card36.setCard(11, 3);
card37.setCard(12, 3);
card38.setCard(13, 3);
card39.setCard(1, 4);
card40.setCard(2, 4);
card41.setCard(3, 4);
card42.setCard(4, 4);
card43.setCard(5, 4);
card44.setCard(6, 4);
card45.setCard(7, 4);
card46.setCard(8, 4);
card47.setCard(9, 4);
card48.setCard(10, 4);
card49.setCard(11, 4);
card50.setCard(12, 4);
card51.setCard(13, 4);
Card [] deck = {card0, card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13,
card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26, card27, card28,
card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39, card40, card41, card42, card43,
card44, card45, card46, card47, card48, card49, card50, card51,
};
deck[].shuffleDeck();
}
}
int索引;
卡片[]甲板;
公共空间{
对于(inti=0;i这不是那样工作的
shuffleDeck()方法不是来自Card[]类。此外,您不应该两次定义“Card[]deck”
您必须将Card[]对象“deck”作为方法shuffleDeck()的参数
请看这里:
public void shuffleDeck(Card[] givenCards){
for (int i = 0; i<givenCards.length; i++){
index = (int)(Math.random()*givenCards.length);
givenCards[index] = givenCards[i];
}
}
如果要在不将所有内容设置为静态的情况下执行方法,则必须创建一个新类并创建其实例。在执行类实例时,必须将要执行的内容放入构造函数中
例如,创建类应用程序
类应用程序:
public class App {
public static void main(String[] args) {
YourClass newClass = new YourClass();
}
}
要运行shuffleDeck(),必须创建Main对象的实例,因为函数属于Main类。此外,还应初始化Main类的deck变量。shuffleDeck
与deck
没有任何关系,当然也不属于deck[]
(无论应该是什么)。如果我将Card[]deck更改为Main[]deck,这是主类的一个deck变量,然后运行它,我会得到完全相同的错误。类需要deck[].shuffleDeck();另外,创建Main对象的实例和初始化主类的deck变量不是一回事吗?
public class App {
public static void main(String[] args) {
YourClass newClass = new YourClass();
}
}