如何将此属性称为if语句?

时间:2013-09-06 21:58:19

标签: c# switch-statement

这是我在Stackoverflow上的第一篇文章,所以对于我如何提出问题/标题以及对实际问题的回答,我将不胜感激。我无法让我的if语句工作。基本上,我试图给所有卡片一个数值和一个颜色值。我无法将cardcolor属性添加到我已经发出的playerCards []中。我放了一行????????标志我遇到麻烦。此外,我不确定是否使用交换语句处理我的卡是正确的方法,使用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;
    }    


    }

}

1 个答案:

答案 0 :(得分: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.");
}

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