[英]Can't access array in the main method
我正在尝试创建一个类,该类以一种方法创建卡片组,而在另一种方法中, 我希望将卡片组洗牌1000次,然后打印到控制台。 (这是我的老师告诉我做的一个实验)我的问题是我在CardDeck方法内创建了数组,但无法在该方法之外访问它们。 我正在寻找一个对于编程新手来说很简单的答案,它可以理解并适用于我当前的代码。 该程序用java编写。
import java.util.Random;
public class Deck {
public static void main(String args[]) {
// mah lovely arrays ^.^
CardDeck();
Random random = new Random();
for (int q = 0; q < 52; q++) {
int rand = random.nextInt(52);
}
}
public static void CardDeck() {
final String[] deckSuit = new String[52];
String[] deckKind = new String[52];
int[] deckValue = new int[52];
String spade = "Spades";
String diamond = "Diamonds";
String heart = "Hearts";
String club = "Clubs";
// set the respective suits
for (int q = 0; q < 13; q++) {
deckSuit[q] = spade;
}
for (int q = 13; q < 26; q++) {
deckSuit[q] = diamond;
}
for (int q = 26; q < 39; q++) {
deckSuit[q] = heart;
}
for (int q = 39; q < 52; q++) {
deckSuit[q] = club;
}
// set the kind of card
for (int q = 0; q < 52; q += 13) {
deckKind[q] = "Two";
deckKind[q + 1] = "Three";
deckKind[q + 2] = "Four";
deckKind[q + 3] = "Five";
deckKind[q + 4] = "Six";
deckKind[q + 5] = "Seven";
deckKind[q + 6] = "Eight";
deckKind[q + 7] = "Nine";
deckKind[q + 8] = "Ten";
deckKind[q + 9] = "Jack";
deckKind[q + 10] = "Queen";
deckKind[q + 11] = "King";
deckKind[q + 12] = "Ace";
}
for (int q = 0; q < 52; q += 13) {
deckValue[q] = 2;
deckValue[q + 1] = 3;
deckValue[q + 2] = 4;
deckValue[q + 3] = 5;
deckValue[q + 4] = 6;
deckValue[q + 5] = 7;
deckValue[q + 6] = 8;
deckValue[q + 7] = 9;
deckValue[q + 8] = 10;
deckValue[q + 9] = 10;
deckValue[q + 10] = 10;
deckValue[q + 11] = 10;
deckValue[q + 12] = 11;
}
//display all the cards
for (int q = 0; q < 52; q++) {
System.out.println("[" + deckSuit[q] + ", " + deckKind[q] + ", " + deckValue[q] + "]");
}
}
//declare ye methods here
}
几个解决方案
使数组静态公开
从函数返回数组
因为您是在CardDeck方法内部创建/声明数组的,所以这就是为什么您不能在该方法之外使用的原因。
您可以将数组声明为实例变量,然后在CardDeck()方法中进行初始化。
第一种解决方案是在更高范围内声明变量。 您可以在Deck类中创建一个静态成员,该成员可以通过任何静态方法(在这种情况下为main()
和CardDeck()
)访问,也可以在main()
方法中创建变量并将其作为参数传递给CardDeck(String[])
方法。
但是我认为一个更好的解决方案是将CardDeck()
方法的原型更改为String[] createDeck()
。 该方法首先创建平台,然后返回它。 然后,在main()
方法中,将返回的值存储在可以在for循环中使用的本地String[]
中。
您的方括号格式不正确。 您在`public static void CardDeck()上方有一个大括号。
固定代码:
import java.util.Random;
public class Deck {
public static void main(String args []){
// mah lovely arrays ^.^
CardDeck();
Random random = new Random();
for(int q = 0;q<52;q++){
int rand = random.nextInt(52);
}
}
public static void CardDeck(){
final String[] deckSuit = new String[52];
String[] deckKind = new String[52];
int[] deckValue = new int[52];
String spade = "Spades";
String diamond = "Diamonds";
String heart = "Hearts";
String club = "Clubs";
// set the respective suits
for(int q = 0;q<13;q++){
deckSuit[q] = spade;
}
for(int q = 13;q<26;q++){
deckSuit[q] = diamond;
}
for(int q = 26;q<39;q++){
deckSuit[q] = heart;
}
for(int q = 39;q<52;q++){
deckSuit[q] = club;
}
// set the kind of card
for(int q = 0;q<52;q+=13){
deckKind[q] = "Two";
deckKind[q+1] = "Three";
deckKind[q+2] = "Four";
deckKind[q+3] = "Five";
deckKind[q+4] = "Six";
deckKind[q+5] = "Seven";
deckKind[q+6] = "Eight";
deckKind[q+7] = "Nine";
deckKind[q+8] = "Ten";
deckKind[q+9] = "Jack";
deckKind[q+10] = "Queen";
deckKind[q+11] = "King";
deckKind[q+12] = "Ace";
}
for(int q = 0;q<52;q+=13){
deckValue[q] = 2;
deckValue[q+1] = 3;
deckValue[q+2] = 4;
deckValue[q+3] = 5;
deckValue[q+4] = 6;
deckValue[q+5] = 7;
deckValue[q+6] = 8;
deckValue[q+7] = 9;
deckValue[q+8] = 10;
deckValue[q+9] = 10;
deckValue[q+10] = 10;
deckValue[q+11] = 10;
deckValue[q+12] = 11;
}
//display all the cards
for(int q = 0;q<52;q++){
System.out.println("["+deckSuit[q]+", "+deckKind[q]+", "+deckValue[q]+"]");
}
//declare ye methods here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.