0

I wrote a code for a simple card dealing program where you can input the number of players that are playing and it will output two cards for each of them plus 5 (the river, flop etc.)

I'm wondering how I can make the m not be able to equal 1, so that if there if the user inputs one it shows an error.

Also, I'm wondering if there's any way to separate each players cards and the 5 cards so it's more visually appealing and give them titles lie "Player 1", "Flop"...

Thanks!

public static void main(String[] args) throws IOException {

     BufferedReader in;
     int x;
     String playerx;

     in = new BufferedReader(new InputStreamReader(System.in));
     System.out.println("How many players are playing?");
     playerx = in.readLine(); //user input for menu selection
     x = Integer.valueOf(playerx).intValue();

     // create a deck of 52 cards and suit and rank sets
     String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
     String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                       "Jack", "Queen", "King", "Ace" };

     //initialize variables
    int suits = suit.length;
    int ranks = rank.length;
    int n = suits * ranks;
    //counter (5 house cards and 2 cards per player entered)
    int m = 5+(x*2); 

     // initialize deck
     String[] deck = new String[n];
     for (int i = 0; i < ranks; i++) { 
         for (int j = 0; j < suits; j++) { 
             deck[suits*i + j] = rank[i] + " of " + suit[j]; 

         }
     }

    // create random 5 cards
    for (int i = 0; i < m; i++)  {
        int r = i + (int) (Math.random() * (n-i));
        String t = deck[r];
        deck[r] = deck[i];
        deck[i] = t;
    }

    // print results
    for (int i = 0; i < m; i++){
        System.out.println(deck[i]);

    }        

}

}

1 Answers1

0

You need to think about what happens if the player DOES enter 1 then. Should it ask again?

If it needs to ask again then, use a loop to continuously ask again until the answer is not 1 any more:

x = 1;
while(x == 1) {
    System.out.println("How many players are playing?");
    playerx = in.readLine(); //user input for menu selection
    x = Integer.valueOf(playerx).intValue();
}
Ghostkeeper
  • 2,830
  • 1
  • 15
  • 27