我需要访问两个String variables
suit
和rank
,
然后在for loop
之外使用它们。这是可能吗?
public class Deck {
public static void main(String[] args) {
int deck[] = new int[52];
String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
String ranks[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
for(int i = 0; i < deck.length; i++) deck[i] = i;
for(int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
// randomizes the deck
for(int i = 1; i < deck.length; i++) {
String suit = suits[deck[i] / 13]; // I want to access these two Strings
String rank = ranks[deck[i] % 13]; // here
System.out.println(rank + "\t" + suit);
}
// and use them here
// I'm trying to make a poker game but need to use those two to deal a hand
}
}
答案 0 :(得分:3)
如果您正在尝试处理手,那么在我看来您需要一个集合 - 毕竟,玩家持有多张牌。
我认为您需要退后一步,而不是继续使用当前的代码。我做这样的事情:
Card
类Deck
课程,用于存储卡片中剩余的卡片#34;从每张卡片中的一张(或更多张)开始Player
类,其中包含一组卡片作为手PokerGame
类(或类似的东西)作为入口点 - 这是具有main
in的类。(它对它没有多大意义)在我看来,请Deck
。)答案 1 :(得分:1)
定义foor-loop之外的变量,如下所示:
String suit;
String rank;
for(int i = 1; i < deck.length; i++) {
suit = suits[deck[i] / 13]; // I want to access these two Strings
rank = ranks[deck[i] % 13]; // here
System.out.println(rank + "\t" + suit);
}
// and use them here
// I'm trying to make a poker game but need to use those two to deal a hand
}