2015-11-02 60 views
0

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

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 
          } 
        } 

      } 
    } 
} 
+0

我不確定我是否理解正確的問題,但對於每一個計數,你是_overwriting_在'甲板'的每個元素 – sam

+0

對不起,如果我不清楚。我需要用戶在開始時指定的「卡組」或全部52種卡類型。所以我正在嘗試執行循環,使用卡填充數組的次數是爲了達到這個目的,但它不起作用,並且只給了我最初的52次,無論我設置循環的次數是多少次跑。 – andrewxt

+0

所以對於2副牌,它的104張牌? – sam

回答

1

看看我對代碼的評論(我刪除了你的評論)。我想,這是你的問題是,

import java.util.Random; 
import java.util.Scanner; 
import java.util.Arrays; 
import java.util.ArrayList; 
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"}; 
     String[] faces = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; 
     String[] deck = new String[52 * numberOfDecks]; 
     boolean deckComplete = false; 
     //random must have the correct number of elements 
     int[] random = new int[52 * numberOfDecks]; 

     for (int x = 0; x<random.length; x++) { 
      random[x] = x; 
     } 

     Random rndNum = new Random(); 

     int i = 0; 

     for (int count = 0; count <= numberOfDecks; count++){ 

      for (i = i; i < deck.length; i++) { 

       System.out.println("i is " + i); 
       //you were doing the same for suit (i/13), but it should be as below 
       deck[i] = faces[i % 13] + suit[i % 4]; 
      } 

      //otherwise you are overiding the same values over and over again for each loop 
      i = i+52; 
     } 

     ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(deck)); 

     while (deckComplete == false) { 

      //depending of number of decks user selects it has more than 52 cards 
      for (int j = deck.length; j >= 1; j--) { 

       //so the user know what to do 
       System.out.println("Just hit enter to contine");  
       String readString = scanner.nextLine(); 
       int randomNumber = rndNum.nextInt(j); 

       if (readString.equals("")) { 

        System.out.println(arrayList.get(random[randomNumber])); 
        arrayList.remove(random[randomNumber]); 

        if (j == 1) { 

         deckComplete = true; 
         System.out.println("You are out of cards!"); 
        } 
       } 
      } 
     } 
    } 
} 
+0

如果您也可以提及更改,這將有所幫助。:) –

+0

@Crazy忍者的變化是在代碼的意見:) –

+0

非常感謝你。我明白我現在做錯了什麼! – andrewxt

1

就像我在評論中說,對於每一個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 

您只需要一個for循環,因爲您的卡組大小已經是52 * numberOfDecks。

另外,請點擊此處:suit[i/13]。這將導致IndexOutOfBoundException我去時,我/ 13會比西裝的尺寸爲4

變化,爲suit[i%4]

0

for (int i = 0; i < deck.length; i++) {

應該更大

for (int i = 52 * count; i < deck.length; i++) {

我需要計數從0到52 * numberOfDecks