samedi 15 avril 2017

Setting random values from an Array into a List till condition is met. in C#

I want to set new random values from an Array into a List till one of two conditions is met. But it's only letting me add another 1 value without considering the condition. After I enter "y" the program letting me to get another value and then it asking me again if I want another card, when I enter "y" again , the code move on without letting me add another value. The second problem is with the if(Hands.playerHand.Sum() > 22) I want the program to calculate the total value of the list and if it's more then 22 then execute the command.

Thank you!

        string userAnotherCard = Console.ReadLine();

        bool secondHand = true;
        secondHand = (userAnotherCard == "y");

        bool secondHandNo = true;
        secondHandNo = (userAnotherCard == "n");

        while(secondHand)
        {

                //if user want another card, this programm will ganerate another card and store it inside playerHand

                    Hands.playerHand.Add(Deck.CardDeck[Hands.rando.Next(0, Deck.CardDeck.Length)]);
                    Console.WriteLine("Your cards are: ");
                    Hands.playerHand.ForEach(Console.WriteLine);

                    Console.WriteLine("Would u like to take another card?");
                    Console.ReadLine();
                 if(Hands.playerHand.Sum() > 22)
                    Console.WriteLine("You loss, your cards sum is more than 21");
                    break;
        }

        while (secondHandNo)
                    break;




Aucun commentaire:

Enregistrer un commentaire