| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 407 人关注过本帖
标题:Head First C#中的一个程序,求大神帮我看看错在哪里了!!
只看楼主 加入收藏
bastard823
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-2-26
结帖率:100%
收藏
 问题点数:0 回复次数:0 
Head First C#中的一个程序,求大神帮我看看错在哪里了!!
这是head first C#里的一道实验题,就是一个赛狗的程序,一共3个类,Guy是下注的人,Greyhound是狗,Bet是下的注。其他的请各位大神自己看下,问题是出在每次用guys.collect()方法都会提示我时候NullReferenceException异常,不知道错误在哪里?求解!!
A Day at the Race.zip (850.89 KB)


程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A_Day_at_the_Race
{
    class Guy
    {
        public string Name;//The guy's name
        public Bet MyBet;//An instance of Bet() that has his bet
        public int Cash;//How much cash he has

        //The last two fields are the guy's GUI controls on the form
        public RadioButton MyRadioButton;//My RadioButton
        public Label MyLabel;//My Label

        public void UpdateLabels()
        {
            MyLabel.Text =this .Name + MyBet.GetDescription();//Set my label to my bet's description.and the label on my
            MyRadioButton.Text = Name + " has " + this.Cash + " bucks";//my radio button to show my cash("Joe has 43 bucks")
        }

        public void ClearBet()
        {
            //Reset my bet so it's zero
            MyLabel.Text = Name + " hasn't place a bet";
        }

        public bool PlaceBet(int Amount, int Dog)
        {
            //Place a new bet and store it in my bet field
            //Return true if the guy had enough money to bet
            MyBet = new Bet() { Amount = Amount, Dog = Dog, Bettor = this };
            if (this.Cash >= Amount)
                return true;
            else
                return false;
        }

        public void Collect(int Winner)
        {
            //Ask my bet to pay out
            this.Cash += MyBet.PayOut(Winner);
            UpdateLabels();
        }
    }
}



程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A_Day_at_the_Race
{
    class Bet
    {
        public int Amount;//The amount of cash that was bet
        public int Dog;//The number of the dog the bet is on
        public Guy Bettor;//The guy who placed the bet
        
        public string GetDescription()
        {
            //Return a string that says who placed the bet,how much
            //cash was bet,and which dog he bet on ("Joe bets 8 on
            //dog #4).If the amount is zero,no bet was placed
            //("Joe hasn't placed a bet").
            if (Amount == 0)
                return " hasn't placed a bet";
            else 
                return " bets " + Amount + " on dog #" + Dog;
        }

        public int PayOut(int Winner)
        {
            //The parameter is the winner of the race.If the dog won,
            //return the amount bet.Otherwise,return the negative of
            //the amount bet.
            if (Dog == Winner)
                return Amount;
            else
                return -Amount;
        }
    }
}


程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows .Forms;
using System.Drawing;

namespace A_Day_at_the_Race
{
    class Greyhound
    {
        public int StartingLocation;//Where my PictureBox start
        public int RacetrackLength=438;//How long the racetrack is
        public PictureBox MyPictureBox = null;//My PictureBox object
        public int Location = 0;//My Location on the racetrack
        public Random Randomizer ;//An instance of Random
        
        public bool Run()
        {
            //Move forward either 1,2,3 or 4 spaces at the random
            //Update the position of my PictureBox on the form
            //Return true if I won the race
            Randomizer = new Random();
            Point p = MyPictureBox.Location;
            p.X += Randomizer.Next(1, 5);
            MyPictureBox.Location = p;
            if (p.X >= RacetrackLength )
                return true;
            else
                return false;
        }

        public void TakeStartingPosition()
        {
            Point p = MyPictureBox.Location;
            p.X = 8;
            MyPictureBox.Location = p;
        }
        
    }
}


程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A_Day_at_the_Race
{
    public partial class Form1 : Form
    {
        Greyhound[] dogs;
        Guy[] guys;
        
        public Form1()
        {
            InitializeComponent();
            dogs = new Greyhound[4];
            dogs[0] = new Greyhound();
            dogs[1] = new Greyhound();
            dogs[2] = new Greyhound();
            dogs[3] = new Greyhound();
            dogs[0].MyPictureBox = pictureBox1;
            dogs[1].MyPictureBox = pictureBox2;
            dogs[2].MyPictureBox = pictureBox3;
            dogs[3].MyPictureBox = pictureBox4;
            guys = new Guy[3];
            guys[0] = new Guy();
            guys[1] = new Guy();
            guys[2] = new Guy();
            guys[0].Name = "Joe";
            guys[0].Cash = 50;
            guys[0].MyLabel = label1;
            guys[0].MyRadioButton = radioButton1;
            guys[1].Name = "Bob";
            guys[1].Cash = 75;
            guys[1].MyLabel = label2;
            guys[1].MyRadioButton = radioButton2;
            guys[2].Name = "Al";
            guys[2].Cash = 45;
            guys[2].MyLabel = label3;
            guys[2].MyRadioButton = radioButton3;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bool check = dogs[0].Run() | dogs[1].Run() | dogs[2].Run() | dogs[3].Run();
            while (check == false)
            {
                for (int i = 0; i <= 3; i++)
                {       
                    dogs[i].Run();
                    System.Threading.Thread.Sleep(50);
                    check = dogs[0].Run() | dogs[1].Run() | dogs[2].Run() | dogs[3].Run();
                    if (check == true)
                    {
                        MessageBox.Show("We have a winner - dog #"+(i+1).ToString());
                        for (int j = 0; j < 3; j++)
                        {
                            guys[j].Collect(i);
                        }
                        break;
                    }
                }   
            }
            for (int k = 0; k < 4; k++)
            {
                dogs[k].TakeStartingPosition();    
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int x = 0;
            switch (label6.Text)
            {
                case "Joe":
                    x = 0;
                    break;
                case "Bob":
                    x = 1;
                    break ;
                case "Al":
                    x = 2;
                    break ;
            }
            if (guys[x].PlaceBet((int)numericUpDown1.Value, (int)numericUpDown2.Value))
                guys[x].UpdateLabels();                        
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            label6.Text = "Joe";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            label6.Text = "Bob";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            label6.Text = "Al";
        }
    }
}
搜索更多相关主题的帖子: 其他 
2012-03-23 13:49
快速回复:Head First C#中的一个程序,求大神帮我看看错在哪里了!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039081 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved