Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为对象的实例-Black Jack游戏_C# - Fatal编程技术网

C# 对象引用未设置为对象的实例-Black Jack游戏

C# 对象引用未设置为对象的实例-Black Jack游戏,c#,C#,我是一个编程新手,我正在制作一个Black Jack游戏,但我真的很难从我的洗牌牌组中向玩家的手中添加一张牌,当我调用Hit函数时,我得到以下消息: 对象引用未设置为对象的实例 我知道解决方案可能非常明显,但您能帮忙吗? 问题就在Deck类的底部,在Hit()函数中 /*主类*/ using System; using System.Collections.Generic; using System.Text; namespace BlackJackGameX { public cla

我是一个编程新手,我正在制作一个Black Jack游戏,但我真的很难从我的洗牌牌组中向玩家的手中添加一张牌,当我调用
Hit
函数时,我得到以下消息:

对象引用未设置为对象的实例

我知道解决方案可能非常明显,但您能帮忙吗?
问题就在
Deck
类的底部,在
Hit()
函数中

/*主类*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class MainClass
    {
        public static void Main (string[] args)
        {

            Deck cards = new Deck();

            Hand playerHand = new Hand(cards);

            Console.WriteLine("Welcome to Black Jack\n\nPress Enter To Start");
            Console.ReadLine ();

            cards.Hit();

            playerHand.PrintHand();

        }
    }
}
/*卡片类*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public enum Suit {Spades, Hearts, Clubs, Diamonds}
    public enum FaceValue {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

    public class Card
    {
        public Suit CardSuit;
        public FaceValue CardValue;
        public int iValue;

        public Card (Suit cardSuit, FaceValue cardValue, int ivalue)
        {
            CardSuit = cardSuit;
            CardValue = cardValue;
            iValue = ivalue;
        }

        public Card ()
        {

        }
    }
}
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class Hand
    {
        public List<Card> PlayerHand;

        private Deck cardDeck = new Deck();

        public Hand (Deck Cards)
        {
            cardDeck = Cards;
        }

        public void PrintHand()
        {
            for (int i = 0; i < PlayerHand.Count; ++i)
            {
                Console.WriteLine("You have a " + PlayerHand[i].CardValue + " of " + PlayerHand[i].CardSuit);

                if (i < PlayerHand.Count)
                {
                    Console.WriteLine ("&");
                }
            }

            Console.ReadLine();

        }
    }
}
/*手工课*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public enum Suit {Spades, Hearts, Clubs, Diamonds}
    public enum FaceValue {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

    public class Card
    {
        public Suit CardSuit;
        public FaceValue CardValue;
        public int iValue;

        public Card (Suit cardSuit, FaceValue cardValue, int ivalue)
        {
            CardSuit = cardSuit;
            CardValue = cardValue;
            iValue = ivalue;
        }

        public Card ()
        {

        }
    }
}
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class Hand
    {
        public List<Card> PlayerHand;

        private Deck cardDeck = new Deck();

        public Hand (Deck Cards)
        {
            cardDeck = Cards;
        }

        public void PrintHand()
        {
            for (int i = 0; i < PlayerHand.Count; ++i)
            {
                Console.WriteLine("You have a " + PlayerHand[i].CardValue + " of " + PlayerHand[i].CardSuit);

                if (i < PlayerHand.Count)
                {
                    Console.WriteLine ("&");
                }
            }

            Console.ReadLine();

        }
    }
}
使用System.Collections.Generic;
使用系统文本;
命名空间BlackJackGameX
{
公务生
{
公开名单玩家;
私人甲板cardDeck=新甲板();
公众手牌(牌组卡)
{
卡片组=卡片;
}
公共空白打印手()
{
for(int i=0;i
/*甲板级*/

using System;
using System.Collections.Generic;
using System.Text;

namespace BlackJackGameX
{
    public class Deck
    {
        Hand playerHand;

        Random rNumber = new Random();

        List<Card> Cards;

        public int iValue1 = 11;
        public int iValue2 = 2;
        public int iValue3 = 3;
        public int iValue4 = 4;
        public int iValue5 = 5;
        public int iValue6 = 6;
        public int iValue7 = 7;
        public int iValue8 = 8;
        public int iValue9 = 9;
        public int iValue10 = 10;
        public int iValue11 = 10;
        public int iValue12 = 10;
        public int iValue13 = 10;

        public Deck()
        {
            Cards = NewDeck();
        }

        private List<Card> NewDeck()
        {
            var AllSuits = new Suit[]
            {
                Suit.Spades,
                Suit.Hearts,
                Suit.Clubs,
                Suit.Diamonds
            };

            var AllFaces = new FaceValue[]
            {
                FaceValue.Ace,
                FaceValue.Two,
                FaceValue.Three,
                FaceValue.Four,
                FaceValue.Five,
                FaceValue.Six,
                FaceValue.Seven,
                FaceValue.Eight,
                FaceValue.Nine,
                FaceValue.Ten,
                FaceValue.Jack,
                FaceValue.Queen,
                FaceValue.King
            };

            var AllValues = new int[]
            {
                iValue1,
                iValue2,
                iValue3,
                iValue4,
                iValue5,
                iValue6,
                iValue7,
                iValue8,
                iValue9,
                iValue10,
                iValue11,
                iValue12,
                iValue13
            };

            List<Card> myDeck = new List<Card>();

            for (int i = 0; i <= 3; i++) 
            {
                for (int j = 0; j <= 12; j++) 
                {
                    Card newCard = new Card(AllSuits[i], AllFaces[j], AllValues[j]);
                    myDeck.Add(newCard);

                }
            }

            List<Card> shuffledDeck = new List<Card> ();

            while (myDeck.Count > 0)
            {
                int c = myDeck.Count;

                int n = rNumber.Next (0, c);

                // var will translate into an int which will be used to pick the card needed from the list
                var value = myDeck[n];
                shuffledDeck.Add(value);
                myDeck.Remove(value);

            }

            return shuffledDeck;
        }

        public void Print()
        {
            for (int i = 0; i <= 51; i++) 
            {
                Console.WriteLine("You drew a " + (Cards [i].CardValue) + " of " + (Cards [i].CardSuit));
                Console.ReadLine ();
            }
        }

        public void Hit()
        {

            playerHand.PlayerHand.Add(Cards[0]);
            Cards.Remove(Cards[0]);

            /*var tmp = Cards[0];
            Cards.Remove(tmp);
            return tmp;*/
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
命名空间BlackJackGameX
{
公务舱甲板
{
手拉手;
Random rNumber=新随机数();
名单卡;
公共int iValue1=11;
公共int iValue2=2;
公共int iValue3=3;
公共int iValue4=4;
公共int iValue5=5;
公共int iValue6=6;
公共int iValue7=7;
公共int iValue8=8;
公共int iValue9=9;
公共int iValue10=10;
公共int iValue11=10;
公共int iValue12=10;
公共int iValue13=10;
公共甲板()
{
Cards=NewDeck();
}
私有列表NewDeck()
{
var allsuites=新西装[]
{
西装,黑桃,
西服,红心,
西装,球杆,
西装,钻石
};
var AllFaces=new FaceValue[]
{
FaceValue.Ace,
FaceValue.2,
FaceValue.3,
FaceValue.4,
FaceValue.5,
FaceValue.6,
FaceValue.7,
FaceValue.8,
FaceValue.9,
FaceValue.10,
FaceValue,Jack,
FaceValue,女王,
FaceValue.King
};
var AllValues=new int[]
{
iValue1,
iValue2,
iValue3,
iValue4,
伊瓦鲁5,
iValue6,
伊瓦鲁7,
伊瓦鲁8,
伊瓦鲁9,
iValue10,
iValue11,
iValue12,
伊瓦鲁13
};
List myDeck=新列表();
对于您手上的(int i=0;i
公共列表PlayerHand;
类从未分配给,并且始终为
null

Hit
中,调用
playerHand.playerHand.Add
-
playerHand.playerHand
null
,因此调用
Add
会导致
null引用异常


通过抛出异常的函数进行调试应该可以清楚地显示这些问题-通过观察所使用的变量,您可以看到哪些变量具有预期值,哪些没有。

看起来像Deck.playerHand从未初始化。Hand.playerHand也一样。

您从未在Deck中设置playerHand,所以呢n您调用hit,playerHand为空。

文件的哪一行触发了错误?
playerHand
dead give away..您没有为该对象创建新的
实例。.下次省去一些麻烦并调试@DJ KRAZE时,我很抱歉要问一些明显的问题,但我与Xamarin Studio的调试器不起作用,我我只做了5个星期的编程。在我试着了解基本知识的时候,恐怕你得耐心等待。谢谢你的帮助。@Jon-给它分配一些东西。如果
手需要一张卡片列表,它从哪里得到的?不幸的是,我使用的是Xamarin Studio,没有工作的调试器。]:手需要拿汽车ds来自组。那么我该如何分配它呢?@JonDunn-你应该能够在
手动
构造函数中完成它,在那里你将一个
传入。我仍然无法确定要添加到手动构造函数中的内容,我添加的任何内容都会给我的代码带来进一步的问题。我想我可能承担了超出我能力范围的任务。可以吗你可能会在代码中告诉我我需要做什么,我真的很难看到解决方案。