简体   繁体   中英

Repeating for loop with user inputted number

Have a program that is able to deal cards from a standard playing deck when the enter button is pressed. Everything works but I need to add functionality to ask the user how many decks they would like to use from the beginning. I have a scanner which takes that input and sets it to a variable.

Scanner scanner = new Scanner(System.in);
int numberOfDecks = scanner.nextInt();

Then this is how I populated the ArrayList with each card

          for (int i = 0; i < deck.length; i++) {
                  deck[i] = faces[i % 13] + suit[i/13];
          }

My idea to implement this feature would be to nest that for loop within another which will run the loop as many times as the value of 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
        }

I tried doing this but for some reason count never resolves to more than 0 no matter the value of numberOfDecks . I am then left with an ArrayList with the correct size but every entry beyond the first 52 are blank because the loop never goes more than once. Can someone see what I'm doing wrong?

EDIT: For clarification, here is the entire program.

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

          }
  }
}

Have a look at my comments on the code (I've removed your comments). I think this is where your problem was,

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!");
                    } 
                }
            }
        }
    }
}

Like I said in the comments, for each count , you are overwriting all the elements of deck.

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

You only need one for loop as you the deck size is already 52 * numberOfDecks.

Also, check here: suit[i/13] . This will lead to IndexOutOfBoundException as i goes larger, i/13 will go larger than size of suit which is 4.

Change that to suit[i%4]

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

should be

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

i needs to count from 0 to 52 * numberOfDecks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM