如何将此属性调用到我的 if 语句

本文关键字:我的 if 语句 调用 属性 | 更新日期: 2023-09-27 18:30:25

这是我在Stackoverflow上的第一篇文章,所以我不胜感激任何关于我如何提出问题/标题以及对我实际问题的回答的评论。我无法让我的 if 语句正常工作。基本上,我正在尝试为所有卡片提供一个数字值和一个颜色值。我在获取我已处理的玩家卡[]的卡颜色属性时遇到问题。我放了一行????????标记我遇到麻烦的地方。此外,我不确定从 switch 语句中处理我的牌是否是使用枚举执行此操作的正确方法更好。请帮帮我!我是新来的,所以要温柔(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;
    }    

    }
}

如何将此属性调用到我的 if 语句

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

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.");
}

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