如何使用 switch case 语句将字母转换为数字
我正在制作一个黑杰克游戏,我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定为什么 switch 会很冗长。
I'm not sure why a switch would be verbose.
有很多方法可以做到这一点,但我可能不会在这里使用开关,因为它会很冗长。这是一种方法:
您可能更喜欢制作
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:
You might prefer to make a
tryParseCard
version to avoid having the exception when the user enters invalid input.