如何使用 switch case 语句将字母转换为数字

发布于 2024-08-21 09:35:05 字数 2000 浏览 6 评论 0原文

我正在制作一个黑杰克游戏,我需要使用 switch case 语句将 A 转换为 11,将 T、Q、J 和 K 转换为 10,但我不知道如何编写代码。有人介意帮我解决这个问题吗?

到目前为止我有:

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 card1 =?");
            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 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 card1 =?");
            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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

临走之时 2024-08-28 09:35:05

我不确定为什么 switch 会很冗长。

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");
    }
}

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");
    }
}
梦里泪两行 2024-08-28 09:35:05

有很多方法可以做到这一点,但我可能不会在这里使用开关,因为它会很冗长。这是一种方法:

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");
}

您可能更喜欢制作 tryParseCard 版本,以避免在用户输入无效输入时出现异常。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文