用用户输入的号码重复循环

时间:2015-11-02 01:07:11

标签: java arrays for-loop arraylist

有一个程序能够在按下输入按钮时从标准游戏牌中处理牌。一切正常但我需要添加功能来询问用户他们想从一开始就使用多少套牌。我有一个扫描仪,它接受输入并将其设置为变量。

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

我实现这个功能的想法是将for循环嵌套在另一个循环中,这将使循环运行的次数与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的值如何,numberOfDecks永远不能解析为0以上。然后我留下一个具有正确大小的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
                          }
                  }

          }
  }
}

3 个答案:

答案 0 :(得分: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!");
                    } 
                }
            }
        }
    }
}

答案 1 :(得分: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随着我越来越大,i / 13将大于4的西装尺寸。

将其更改为suit[i%4]

答案 2 :(得分:0)

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

应该是

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

我需要从0到52 * numberOfDecks

计数