none
Basic if statements assignment RRS feed

  • Question

  • Create a program that generates 5 random numbers between and including 1 and 52. Like picking 5 cards out of a deck. Numbers 1-13 will represent hearts, 14-26 will represent diamonds, 27-39 will represents spades and 40 – 52 will represent clubs. Suits will be represented by the following letters C= clubs, H = hearts, S=spades, D=diamonds Face will be represented by the following A,2,3,4,5,6,7,8,9,10,J,Q,K Output 2H, AC, JH, 4C, 2S You have a pair of 2s or Sorry, no pairs

    approach was to declare a variable for each card number and each randomized face number and the output for the face. it looks like this.

    Random randomGenerator = new Random();

    int number1 = randomGenerator.Next(1, 53);

    int number2 = randomGenerator.Next(1, 53);i

    nt number3 = randomGenerator.Next(1, 53);int number4 = randomGenerator.Next(1, 53);int number5 = randomGenerator.Next(1, 53);int oneFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = Kint twoFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = Kint threeFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = Kint fourFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = Kint fiveFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = Kchar Suits;string resultOneFace;string resultTwoFace;string resultThreeFace;string resultFourFace;string resultFiveFace;


    but my code is extremely long and i cannot figure out how to determine if i have a pair or not.
    Saturday, October 6, 2018 1:12 AM

All replies

  • Create a program that generates 5 random numbers between and including 1 and 52. ... Output 2H, AC, JH, 4C, 2S You have a pair of 2s or Sorry, no pairs

    ...

    but my code is extremely long and i cannot figure out how to determine if i have a pair or not.

    >my code is extremely long

    Then shorten it. Use compound statements where possible. Use methods
    where possible to avoid replicating code. Use lambdas instead of methods
    when that makes for more compact code. Declare variables, constants and
    literals of the same type on a single line instead of multiple lines, etc.

    >i cannot figure out how to determine if i have a pair or not.

    If two - and only two - of the cards have the same value - then it's a pair.

    - Wayne

    Saturday, October 6, 2018 1:51 AM
  • Hi mariedaring,

    Here is a sample about how to determine if it has a pair or not by using LINQ.

    Random randomGenerator = new Random();
                List<int> list = new List<int>();
                int number1 = randomGenerator.Next(1, 53);
                int result1 = number1 % 13;
    
                int number2 = randomGenerator.Next(1, 53);
                int result2 = number1 % 13;
               
                int number3 = randomGenerator.Next(1, 53);
                int result3 = number1 % 13;
                int number4 = randomGenerator.Next(1, 53);
                int result4 = number1 % 13;
                int number5 = randomGenerator.Next(1, 53);
                int result5 = number1 % 13;
                list.Add(result1);
                list.Add(result2);
                list.Add(result3);
                list.Add(result4);
                list.Add(result5);
                int oneFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = K
                int twoFace = randomGenerator.Next(1, 14);  // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = K
                int threeFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = K
                int fourFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = K
                int fiveFace = randomGenerator.Next(1, 14); // 1=A, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=J, 12= Q, 13 = K
                var pair = list.GroupBy(x => x)
                             .Where(group => group.Count() > 1)
                             .Select(group => group.Key);
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 8, 2018 3:31 AM
    Moderator
  • You don't need the separate random numbers for each face. The face of each card is implied by the number between 1 and 52.

    Examples : 16 = 3 of Diamonds, 27 = Ace of Spades

    Monday, October 8, 2018 3:48 AM