纸牌游戏,为什么switch语句的一部分工作而另一部分没有?

时间:2013-10-16 19:48:56

标签: c# switch-statement

所以我正在写一些卡片游戏来学习一些C#。这是我的第一个项目,我遇到了一些麻烦。所以游戏只会问你一些简单的问题。下一张卡是红色还是黑色,是下一张卡高于或低于前一张卡,以及下一张卡在前两张卡的内部或外部。最后,为下一张卡挑选一套西装。它的全部工作除了in out部分。我正在使用switch语句来获取userinput,然后使用if语句来提出正确的答案。如果你打开“in”它会完美运行但如果你输入“out”则不正确请帮助。此外,任何批评都会很好。谢谢你的帮助。

    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
{
    public static void Main()
    {
        Deck deck = new Deck();
        Card card = new Card();
        Intro intro = new Intro();
        Game drink = new Game();
        deck.Shuffle();
        intro.intro();
        drink.game();
        //Console.WriteLine(deck.TakeCard());
        Console.ReadLine();
    }


        //In out part of the  Game
        Card inOut = new Card();
        deck.Shuffle();
        inOut = deck.TakeCard();
        Start2:
        Console.WriteLine("Is the next card going to be inside, out, or equal to \n {0} & {1}
        (in, out or equal)  \n", rorbcard,highLow);
        string userValue2 = Console.ReadLine().ToLower();
        Console.WriteLine(inOut.ToString());
        switch (userValue2)
        {
            case "out":
                          //+                      -
                    if (rorbcard.CardNumber > highLow.CardNumber)
                    {
                        if (inOut.CardNumber > rorbcard.CardNumber) & (inOut.CardNumber <
                        highLow.CardNumber)
                        {
                            Console.WriteLine("Correct give 6 drinks\n");
                        }
                        else
                        {
                            Console.WriteLine("Wrong take 6 drinks\n");
                        }
                    }
                    else if (rorbcard.CardNumber < highLow.CardNumber)
                    {
                        if (inOut.CardNumber < rorbcard.CardNumber) & (inOut.CardNumber >
                        highLow.CardNumber)
                        {
                            Console.WriteLine("Correct give 6 drinks\n");
                        }
                        else
                        {
                            Console.WriteLine("Wrong take 6 drinks\n");
                        }
                    }
                break;
            case "in":
                    if (rorbcard.CardNumber > highLow.CardNumber)
                    {
                        if (inOut.CardNumber < rorbcard.CardNumber & inOut.CardNumber >
                        highLow.CardNumber)
                        {
                            Console.WriteLine("Correct give 6 drinks\n");
                        }
                        else
                        {
                            Console.WriteLine("Wrong take 6 drinks\n");
                        }
                    }
                    else if (rorbcard.CardNumber < highLow.CardNumber)
                    {
                        if (inOut.CardNumber > rorbcard.CardNumber && inOut.CardNumber <
                         highLow.CardNumber)
                        {
                            Console.WriteLine("Correct give 6 drinks\n");
                        }
                        else
                        {
                            Console.WriteLine("Wrong take 6 drinks\n");
                        }
                    }
                break;
            case "equal":

                    if (inOut.CardNumber == rorbcard.CardNumber || inOut.CardNumber ==
                    highLow.CardNumber)
                    {
                        Console.WriteLine("Correct give 6 drinks\n");  
                    }
                    else
                    {
                        Console.WriteLine("Bold move, but wrong drink 6\n");
                    }
                break;
            default:
                {
                    Console.WriteLine("You must input in, out, or equal");
                    goto Start2;
                }
        }
 public enum Suit
{
    Spades = 0,
    Hearts = 1,
    Diamonds = 2,
    Clubs = 3,
}

public enum CardNumber
{
    Two = 1,
    Three = 2,
    Four = 3,
    Five = 4,
    Six = 5,
    Seven = 6,
    Eight = 7,
    Nine = 8,
    Ten = 9,
    Jack = 10,
    Queen = 11,
    King = 12,
    Ace = 13,
}

public class Card
{
    public Suit Suit { get; set; }
    public CardNumber CardNumber { get; set; }

    public override string ToString()
    {
        return CardNumber + " of " + Suit;
    }


}

public class Deck
{

    public Deck()
    {
        Reset();
    }

    public List<Card> Cards { get; set; }

    public void Reset()
    {
        Cards = Enumerable.Range(0, 3).SelectMany(s => Enumerable.Range(1, 13).Select(c => new
        Card()
        {
            Suit = (Suit)s,
            CardNumber = (CardNumber)c
        } )).ToList();
    }

    public void Shuffle()
    {
        Cards = Cards.OrderBy(c => Guid.NewGuid())
                     .ToList();
    }

    public Card TakeCard()
    {
        var card = Cards.FirstOrDefault();
        Cards.Remove(card);
        return card;


    }

    public IEnumerable<Card> TakeCards(int numberOfCards)
    {
        var cards = Cards.Take(numberOfCards);

        var takeCards = cards as Card[] ?? cards.ToArray();
        Cards.RemoveAll(takeCards.Contains);

        return takeCards;
    }
}
}

2 个答案:

答案 0 :(得分:1)

在确定卡是否在“范围之外”时,您的问题似乎与您的逻辑有关。目前,您的内部if语句基本上说:“如果卡片小于较小的卡大于较大的卡片”,这显然是不可能的,而是您想要断言使用布尔OR(||)运算符,该卡小于较大的卡大于较大的卡:

...
if (rorbcard.CardNumber > highLow.CardNumber)
{
    if (inOut.CardNumber > rorbcard.CardNumber) || (inOut.CardNumber < highLow.CardNumber)
    {
        Console.WriteLine("Correct give 6 drinks\n");
    }
    else
    {
        Console.WriteLine("Wrong take 6 drinks\n");
    }
}
else if (rorbcard.CardNumber < highLow.CardNumber)
{
    if (inOut.CardNumber < rorbcard.CardNumber) || (inOut.CardNumber > highLow.CardNumber)
    {
        Console.WriteLine("Correct give 6 drinks\n");
    }
    else
    {
        Console.WriteLine("Wrong take 6 drinks\n");
    }
}
break;
...

请注意,正如其他人所提到的,当AND或OR的两边都是bool类型时,您应该使用条件运算符&&||,而不是逻辑{{ 1}}和&

答案 1 :(得分:0)

我注意到你的情况下有更多的括号。这可能会改变代码的行为。