1

The following code provides the right output but not exactly in the same format that i need it in.

Current code:

public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
    Deck deck = new Deck();
    Card C;

    System.out.println(deck.getTotalCards());
    System.out.print("Number of players: ");
    int players = scan.nextInt();
    System.out.print("Number of cards: ");
    int cards = scan.nextInt();

    for(int k=0; k<players; ++k)
    {
        for(int i =0; i<cards; i++)
        {
            C = deck.drawFromDeck();
            System.out.println("Player "+(k+1) + ": " + C.toString());
        }
    }
    scan.close();
}

This provides this output:

51 // number of cards in deck

Number of players: 3 //3 - entered by user
Number of cards: 2 // 2- entered by user
Player 1: 4 of Spades
Player 1: Jack of Diamonds
Player 2: 6 of Clubs
Player 2: 2 of Hearts
Player 3: Jack of Clubs
Player 3: 8 of Diamonds

Desired output:

51
Number of players: 3 //3 - entered by user
Number of cards: 2 // 2- entered by user
Player 1: 4 of Spades, Jack of Diamonds
Player 2: 6 of Clubs, 2 of Hearts
Player 3: Jack of Clubs, 8 of Diamonds

How can i alter my for-loop to reflect my desired output.

5
  • If you're allowed to use arrays, then use two-dimensional array where row # n will hold all the cards of Player # n. Then use this array to print result. So you use your original nested loop to store the results and then create an additional one to print them out. Commented Mar 10, 2013 at 23:42
  • 1
    If the output is the only thing needed to be formatted, just mess around with print and println Commented Mar 10, 2013 at 23:42
  • @A--C that won't work because print will output all results for all players just for Player 1 Commented Mar 10, 2013 at 23:45
  • @PM77-1 how can i implement this with my code. So i store the results from the first loop and then store results from second loop then combine both? Commented Mar 10, 2013 at 23:47
  • @Amina - Actually you do not need two-dimensional array. You can use one-dimensional just for a single player. Or you do not have to use arrays at all. Just follow the logic already suggested in posted answers: print "Player" in outer loop and "cards" in inner loop. Commented Mar 10, 2013 at 23:52

3 Answers 3

2

You need to use System.out.print() instead of System.out.println() if you don't want a new line to be printed automatically. I think something like this would do it:

for(int k=0; k<players; ++k)
{
    System.out.print("Player "+(k+1) + ": ");
    for(int i =0; i<cards; i++)
    {
        C = deck.drawFromDeck();
        System.out.print(C.toString());
        if(cards >= 2 && i != cards - 2) {
            System.out.print(", ");
        }
    }
    System.out.print("\n");
}
Sign up to request clarification or add additional context in comments.

Comments

1

What you need to do is save the output for each player in a String, and then print it outside the inner loop. So something like:

for(int k=0; k<players; ++k)
    {
        String output = " ";
        for(int i =0; i<cards; i++)
        {
            C = deck.drawFromDeck();
            output+= C.toString() + ",";
        }
        System.out.print("Player " + (k+1) + ": ");
        System.out.println(output);
    }

Comments

1

Inside the inner loop, use System.out.print() instead of System.out.println(). At the end of the outer loop, call System.out.println() to start the next output on the next line.

For example:

for (int k = 0; k < players; k++) {
  for (int i = 0; i < cards; i++) {
    if (i == 0) {
      System.out.print("Player "+(k+1) + ": ");
    }
    else {
      System.out.print(",");
    }
    C = deck.drawFromDeck();
    System.out.print(C.toString());
  }
  System.out.println();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.