纸牌游戏,为什么开关语句的一部分有效,而另一部分不起作用
本文关键字:一部分 有效 不起作用 另一部 为什么 开关 语句 纸牌游戏 | 更新日期: 2023-09-27 18:35:44
所以我正在写一个小纸牌游戏来学习一些C#。这是我的第一个项目,我遇到了一点麻烦。所以游戏只是问你几个简单的问题。是下一张牌是红色还是黑色,是下一张牌高于还是低于前一张牌,下一张牌是前两张牌的内侧还是外侧。最后,为下一张牌挑选一套花色。除了输入部分外,它的所有工作都有效。我正在使用 switch 语句来获取用户输入,然后使用 if 语句来踢出正确答案。如果您打开"输入",它会完美运行,但如果您输入"输出",它会不正确,请帮助。此外,任何批评都会很好。谢谢你的帮助。
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;
}
}
}
看起来您的问题在于确定卡是否"超出"范围的逻辑。目前,你的内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
类型时,您应该使用条件运算符 &&
和 ||
,而不是逻辑&
和|
。
我注意到你的例子中有更多的括号。这可能会改变 de 代码的行为。