2015-11-02 65 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