View Single Post
  #1 (permalink)  
Old August 5th, 2006, 07:37 AM
webworldman webworldman is offline
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default About Ch10 Exercise 4

Yes, I have downloaded and read the answer to this question. And then I thought of another way to solve it. I tried to display five random cards with my new code. But it always displays the first five,that it always flush! Anyone please tell me why?

Question: Write a console client for the Ch10CardLib library that draws five cards at a time from a shuffled Deck object. If all five cards are the same suit, then the client should display the card names on screen along with the text Flush!; otherwise, it should quit after 50 cards with the text No flush.

my new code:

using System;
using System.Collections.Generic;
using System.Text;
using Ch10CardLib;


namespace Ch10Exercise04
{
    class Program
    {
        static void Main(string[] args)
         {

            Deck theNewDeck = new Deck();
            theNewDeck.Shuffle();
            Card[] chosenCards = new Card[5];
            Random randomNum = new Random();
            bool[] cardChosen = new bool[52];
            for (int i = 0; i < 5; i++)
            {
                int tempInt = 0;
                bool tempBool = false;
                while (tempBool == false)
                {
                    tempInt = randomNum.Next(52);
                    if (cardChosen[tempInt] == false)
                        tempBool = true;
                }
                chosenCards[i] = theNewDeck.GetCard(tempInt);
                cardChosen[tempInt] = true;
            }
            foreach (Card chosenCard in chosenCards)
            {
                Console.WriteLine(chosenCard.ToString());
            }
            Console.ReadKey();

        }
    }
}




Reply With Quote