开发者

How do you use a switch case statement to convert Letters into numbers

开发者 https://www.devze.com 2022-12-20 14:52 出处:网络
I am making a black jack game and I need to use a switch case statement to convert A to 11, and T, Q, J, and K to 10, however I am not sure how to do the code. Would someone mind helping me with this

I am making a black jack game and I need to use a switch case statement to convert A to 11, and T, Q, J, and K to 10, however I am not sure how to do the code. Would someone mind helping me with this problem?

So far I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            Console.WriteLine("Welcome to Black Jack!\n");
            Console.WriteLine("Pick two cards to add to your hand\n");
            Console.WriteLine("Cards 2, 3, 4, 5, 6, 7, 8, 9 all are worth face value\n");
            Console.WriteLine("Ace (A) is worth 11,Ten (T), Jack (J), Queen (Q), and King (K) are all worth 10 points\n");
            Console.WriteLine("Whichever sum is higher, that player is declared the winner\n");
            Console.WriteLine("What are player one's cards?");
            Console.WriteLine("Enter car开发者_StackOverflowd1 =?");
            Console.WriteLine("Enter card2 =?");
            double card1 = Double.Parse(Console.ReadLine());
            double card2 = Double.Parse(Console.ReadLine());
            Console.WriteLine("You entered: [" + card1, card2 + "]");
            Console.WriteLine("What are player two's cards?");
            Console.WriteLine("Enter card3 =?");
            Console.WriteLine("Enter card4 =?");
            double card3 = Double.Parse(Console.ReadLine());
            double card4 = Double.Parse(Console.ReadLine());
            Console.WriteLine("You entered: [" + card3, card4 + "]");
            Console.ReadLine();

             {

                Console.WriteLine("Calculate player 1: [" + "card1 + card2" + "]");
                Console.WriteLine("Calculate player 2: [" + "card3 + card4" + "]");
                {
                    if (card1 + card2 > card3 + card4)
                        Console.WriteLine("Player One Wins!");
                    else if (card3 + card4 > card1 + card2)
                        Console.WriteLine("Player Two Wins!");
                    Console.ReadLine();


I'm not sure why a switch would be verbose.

int parseCard(char card)
{
    if ( card >= '2' && card <= '9' ) {
        return card - '0';
    }

    switch ( card ) {
        case 'T':
        case 'J':
        case 'Q':
        case 'K':
            return 10;

         case 'A':
            return 11;

        default:
            throw new ArgumentException("card not valid", "card");
    }
}


There are lots of ways to do this, but I probably wouldn't use a switch here as it would be verbose. This is one way to do it:

int parseCard(char card)
{
    if (card >= '2' && card <= '9')
        return card - '0';
    if (card == 'T' || card == 'J' || card == 'Q' || card == 'K')
        return 10;
    if (card == 'A')
        return 11;
    throw new ArgumentException("card not valid", "card");
}

You might prefer to make a tryParseCard version to avoid having the exception when the user enters invalid input.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号