有一個程序能夠在按下輸入按鈕時從標準播放卡片上發牌。一切正常,但我需要添加功能,從一開始就詢問用戶他們想要使用多少套牌。我有一臺掃描儀,它接受輸入並將其設置爲一個變量。使用用戶輸入的編號重複循環
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