有一个程序能够在按下输入按钮时从标准播放卡片上发牌。一切正常,但我需要添加功能,从一开始就询问用户他们想要使用多少套牌。我有一台扫描仪,它接受输入并将其设置为一个变量。使用用户输入的编号重复循环
Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();
那么这就是我如何填充ArrayList中,每个卡
for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
}
我的想法来实现此功能将巢,对中的另一个循环,这将运行循环多次的值为numberOfDecks
。
for (int count = 0; count <= numberOfDecks; count++){
for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
} //Creates array with all possible cards in standard deck of cards
}
我想这样做,但出于某种原因count
从未解析为大于0不管的numberOfDecks
值。然后,我剩下一个ArrayList,其大小正确,但是第52个以外的每个条目都是空白的,因为循环永远不会超过一次。有人可以看到我做错了什么吗?
编辑:澄清,这里是整个程序。
public class Card {
public static void main(String[] args) {
System.out.println("How many decks would you like to use?");
Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();
String[] suit = {" of Diamonds", " of Spades", " of Hearts", " of Clubs"}; //Array of suits
String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};//Array of face values
String[] deck = new String[52 * numberOfDecks];//Array of actual deck
boolean deckComplete = false;//Boolean for finished deck
int[] random = new int[52]; //Array with all possible numbers between 1-52
for (int x = 0; x<random.length; x++) {
random[x] = x;
} //Fills array with all possible numbers between 1-52
Random rndNum = new Random();
for (int count = 0; count <= numberOfDecks; count++){
for (int i = 0; i < deck.length; i++) {
deck[i] = faces[i % 13] + suit[i/13];
} //Creates array with all poassible cards in standard deck of cards
}
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(deck)); //Converts above array into ArrayList
while (deckComplete == false) {
for (int i = 52; i >= 1; i--) {
String readString = scanner.nextLine();
int randomNumber = rndNum.nextInt(i);
if (readString.equals("")) {
System.out.println(arrayList.get(random[randomNumber]));
arrayList.remove(random[randomNumber]);
if (i == 1) {
deckComplete = true;
System.out.println("You are out of cards!");
} //Deals out random card from deck and removes each one used
}
}
}
}
}
我不确定我是否理解正确的问题,但对于每一个计数,你是_overwriting_在'甲板'的每个元素 – sam
对不起,如果我不清楚。我需要用户在开始时指定的“卡组”或全部52种卡类型。所以我正在尝试执行循环,使用卡填充数组的次数是为了达到这个目的,但它不起作用,并且只给了我最初的52次,无论我设置循环的次数是多少次跑。 – andrewxt
所以对于2副牌,它的104张牌? – sam