1

这是我在 Stackoverflow 上的第一篇文章,因此我将不胜感激任何关于我如何提出问题/标题以及对我的实际问题的回答的评论。我无法让我的 if 语句正常工作。基本上,我试图给所有卡片一个数字值和一个颜色值。我无法将 cardcolor 属性添加到我已分发的 playerCards[] 中。我放了一行?????????标记我遇到麻烦的地方。此外,我不确定从 switch 语句中分发我的卡片是否是使用 Enum 更好的正确方法。请帮助我!我是新人所以要温柔(j/k)

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

namespace Drinking_Game_
{
    class Program
    {
    static string redOrBlack = "";
    static string highOrLow = "";
    static string pickASuit = "";
    static string yesOrNo = "";
    static int total = 0, count = 1;
    static int cardColorBlack = 1, cardColorRed = 0;
    static string cardSuit = "";
    static Random cardRandomizer = new Random();
    static string[] playerCards = new string[4];



    static void Main(string[] args)
    {

        Console.Title = "The Made up Game";
        Start();

    }
    static void Start()
    {
        do
        {
            Console.WriteLine("So you wanna play a  game? Yes or No" );
            yesOrNo = Console.ReadLine().ToLower();
            if (yesOrNo.Equals("no")) 
            {
                Console.WriteLine("You have to enter Yes or No");

            }
        } while (!yesOrNo.Equals("yes") ); //&& !yesOrNo.Equals("no")
        Console.WriteLine("Allright then lets play! Red or Black?");
        redOrBlack = Console.ReadLine().ToLower();
        //Console.ReadLine();
        Game();
        Console.ReadLine();
    }

    static void Game()
    {
        playerCards[0] = Deal();
        Console.WriteLine(" You were dealt a {0}", playerCards[0]);
        ColorChoice();
    }

    static void ColorChoice()
    {


        if (????????????????????)
        {
            Console.WriteLine("Give 2");
        }
        else
        {
            Console.WriteLine("Take 2");
        }

    }

    static string Deal()
    {
        string Card = "";
        int cards = cardRandomizer.Next(1, 53);
        switch (cards)
        {
            case 1: Card = "Two of Diamonds"; total += 2; cardColorRed = 0;
                break;
            case 2: Card = "Three of Diamonds"; total += 3; cardColorRed = 0;
                break;
            case 3: Card = "Four of Diamonds"; total += 4; cardColorRed = 0;
                break;
            case 4: Card = "Five of Diamonds"; total += 5; cardColorRed = 0;
                break;
            case 5: Card = "Six of Diamonds"; total += 6; cardColorRed = 0;
                break;
            case 6: Card = "Seven of Diamonds"; total += 7; cardColorRed = 0;
                break;
            case 7: Card = "Eight of Diamonds"; total += 8; cardColorRed = 0;
                break;
            case 8: Card = "Nine of Diamonds"; total += 9; cardColorRed = 0;
                break;
            case 9: Card = "Ten of Diamonds"; total += 10; cardColorRed = 0;
                break;
            case 10: Card = "Jack of Diamonds"; total += 10; cardColorRed = 0;
                break;
            case 11: Card = "Queen of Diamonds"; total += 10; cardColorRed = 0;
                break;
            case 12: Card = "King of Diamonds"; total += 10; cardColorRed = 0;
                break;
            case 13: Card = "Ace of Diamonds"; total += 11; cardColorRed = 0;
                break;
            case 14: Card = "Two of Hearts"; total += 2; cardColorRed = 0;
                break;
            case 15: Card = "Three of Hearts"; total += 3; cardColorRed = 0;
                break;
            case 16: Card = "Four of Hearts"; total += 4; cardColorRed = 0;
                break;
            case 17: Card = "Five of Hearts"; total += 5; cardColorRed = 0;
                break;
            case 18: Card = "Six of Hearts"; total += 6; cardColorRed = 0;
                break;
            case 19: Card = "Seven of Hearts"; total += 7; cardColorRed = 0;
                break;
            case 20: Card = "Eight of Hearts"; total += 8; cardColorRed = 0;
                break;
            case 21: Card = "Nine of Hearts"; total += 9; cardColorRed = 0;
                break;
            case 22: Card = "Ten of Hearts"; total += 10; cardColorRed = 0;
                break;
            case 23: Card = "Jack of Hearts"; total += 10; cardColorRed = 0;
                break;
            case 24: Card = "Queen of Hearts"; total += 10; cardColorRed = 0;
                break;
            case 25: Card = "King of Hearts"; total += 10; cardColorRed = 0;
                break;
            case 26: Card = "Ace of Hearts"; total += 11; cardColorRed = 0;
                break;
            case 27: Card = "Two of Spades"; total += 2; cardColorBlack = 1;
                break;
            case 28: Card = "Three of Spades"; total += 3; cardColorBlack = 1;
                break;
            case 29: Card = "Four of Spades"; total += 4; cardColorBlack = 1;
                break;
            case 30: Card = "Five of Spades"; total += 5; cardColorBlack = 1;
                break;
            case 31: Card = "Six of Spades"; total += 6; cardColorBlack = 1;
                break;
            case 32: Card = "Seven of Spades"; total += 7; cardColorBlack = 1;
                break;
            case 33: Card = "Eight of Spades"; total += 8; cardColorBlack = 1;
                break;
            case 34: Card = "Nine of Spades"; total += 9; cardColorBlack = 1;
                break;
            case 35: Card = "Ten of Spades"; total += 10; cardColorBlack = 1;
                break;
            case 36: Card = "Jack of Spades"; total += 10; cardColorBlack = 1;
                break;
            case 37: Card = "Queen of Spades"; total += 10; cardColorBlack = 1;
                break;
            case 38: Card = "King of Spades"; total += 10; cardColorBlack = 1;
                break;
            case 39: Card = "Ace of Spades"; total += 11; cardColorBlack = 1;
                break;
            case 40: Card = "Two of Clubs"; total += 2; cardColorBlack = 1;
                break;
            case 41: Card = "Three of Clubs"; total += 3; cardColorBlack = 1;
                break;
            case 42: Card = "Four of Clubs"; total += 4; cardColorBlack = 1;
                break;
            case 43: Card = "Five of Clubs"; total += 5; cardColorBlack = 1;
                break;
            case 44: Card = "Six of Clubs"; total += 6; cardColorBlack = 1;
                break;
            case 45: Card = "Seven of Clubs"; total += 7; cardColorBlack = 1;
                break;
            case 46: Card = "Eight of Clubs"; total += 8; cardColorBlack = 1;
                break;
            case 47: Card = "Nine of Clubs"; total += 9; cardColorBlack = 1;
                break;
            case 48: Card = "Ten of Clubs"; total += 10; cardColorBlack = 1;
                break;
            case 49: Card = "Jack of Clubs"; total += 10; cardColorBlack = 1;
                break;
            case 50: Card = "Queen of Clubs"; total += 10; cardColorBlack = 1;
                break;
            case 51: Card = "King of Clubs"; total += 10; cardColorBlack = 1;
                break;
            case 52: Card = "Ace of Clubs"; total += 11; cardColorBlack = 1;
                break;
            default: Card = "2 of Diamonds"; total += 2; cardColorBlack = 1;
                break;
        }
        return Card;
    }    


    }

}
4

1 回答 1

1

您的程序设计存在多个问题,但解决所有问题超出了此答案的范围。您很可能会遇到问题,因为您使用一个字段来跟踪卡片是否为黑色,而另一个字段用于跟踪它是否为红色,但您永远不会更改它们的值。您最好将它们组合成一个字段,然后在发新牌时适当地设置它。

private const int BLACK = 1;
private const int RED = 2;

private static int cardColor = 0;

...

string Card = "";
int cards = cardRandomizer.Next(1, 53);
switch(cards)
{
    case 1:
        Card = "Two of Diamonds"; total += 2; cardColor = RED;
        break;
...
}

这使得以后更容易检查颜色:

if (cardColor == RED)
{
    Console.WriteLine("Give 2");
}
else if(cardColor == BLACK)
{
    Console.WriteLine("Take 2");
}
else
{
    Console.WriteLine("Unknown card color.");
}

请注意,编写这种类型的程序有很多更好的方法,但这应该足以满足您的需求。

于 2013-09-06T22:20:56.683 回答