头先卡在C#lab1上

头先卡在C#lab1上,c#,winforms,C#,Winforms,我正在翻阅头一本C#book,我被困在了第一个实验室。我几乎所有的东西都准备好了,但我遇到了一些困难,我正在努力克服 我似乎无法让Guy中的myBet.GetDescription()正常工作,即使看起来所有的部分都在那里 当我点击race按钮时,它就好像卡在一个无限循环中。几乎与赛车一样,tracklength没有终点 非常感谢您的帮助 这是我到目前为止的代码 灰狗.cs using System; using System.Collections.Generic; using System.

我正在翻阅头一本C#book,我被困在了第一个实验室。我几乎所有的东西都准备好了,但我遇到了一些困难,我正在努力克服

  • 我似乎无法让
    Guy
    中的
    myBet.GetDescription()
    正常工作,即使看起来所有的部分都在那里

  • 当我点击race按钮时,它就好像卡在一个无限循环中。几乎与
    赛车一样,tracklength
    没有终点

  • 非常感谢您的帮助

    这是我到目前为止的代码

    灰狗.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace WindowsFormsApplication6
    {
        class Greyhound
        {
    
            public int StartingPosition; //Where my picturebox starts
            public int RaceTrackLength; //how long the racetrack is
            public PictureBox myPictureBox = null; //my picturebox object
            public int Location = 0; //My location on racetrack
            public Random Randomizer; //instance of Random
    
    
    
            public bool Run() //use randomizer like in the sub shop example
            {
                //Random Randomizer = new Random();
    
                int randomMovement = Randomizer.Next(1, 4);
    
                Point currentPos = myPictureBox.Location;
                currentPos.X += randomMovement;
                myPictureBox.Location = currentPos;
    
                        //Move forward either 1,2, 3, or 4 spaces at random
                        //update the position of my picture box on form
                        //return true if I won the race
    
                if (currentPos.X >= RaceTrackLength)
                {
                    return true;
                }
                else
                    return false;
    
            }
    
            public void TakeStartingPosition()
            {
                //reset location to the start line
                myPictureBox.Left = StartingPosition;
                Location = 0;
            }
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        class Guy
        {
    
            public string name; //the guy's name
            public Bet myBet = null; //an instance of Bet() that has his bet
            public int cash; //how much cash he has
    
            public RadioButton myRadioButton;
            public Label myLabel;
    
            public void UpdateLabels()
            {
                //set my label to my bet's description, and the label on my
                // radio button to show my cash ('Joe has 43 bucks')
    
                if (myBet == null)
                {
                    myLabel.Text = name + " hasn't placed a bet";
                }
                else {
                    myLabel.Text = myBet.GetDescription();
                    //Why isn't this working??
                }
    
                myRadioButton.Text = name + " has " + cash + " bucks";
    
            }
            public void ClearBet() 
            {
                //clear the betting table
                myBet.amount = 0;
            }
    
            public bool PlaceBet(int amount, int Dog)
            {
                Bet myBet = new Bet();
                    myBet.Dog = Dog;
                    myBet.amount = amount;
                    myBet.Bettor = this;
                    UpdateLabels();
    
    
    
                //place a new bet and store it in my bet field
                //return true if the guy had enough money to bet
    
                if (amount >= cash)
                {
                    return false;
                }
                else
                    return true;
            }
    
            public void Collect(int Winner) {
                cash += myBet.PayOut(Winner);
            } //ask my bet to pay out
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication6
    {
        class Bet
        {
            public int amount; //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 Bettor.name + " bets " + amount + "on dog #" + Dog;
                }
                else
                    return Bettor.name + " has not placed a bet!";
            }
    
            public int PayOut(int Winner)
            {
    
                //the parameter is the winner of the race. if the dog won, 
                //returns 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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
    
            //initialize dog and guy arrays
            Greyhound[] dogs = new Greyhound[4];
            Guy[] guys = new Guy[3];
            Random myRandom = new Random();
    
    
            public Form1()
            {
                InitializeComponent();
    
                //guys object initialization
                guys[0] = new Guy()
                {
                    cash = 50,
                    myLabel = joeBetLabel,
                    myRadioButton = joeRadioButton,
                    name = "Joe"
    
                };
    
                guys[1] = new Guy()
                {
                    cash = 75,
                    myLabel = bobBetLabel,
                    myRadioButton = bobRadioButton,
                    name = "Bob"
                };
    
                guys[2] = new Guy()
                {
                    cash = 45,
                    myLabel = alBetLabel,
                    myRadioButton = alRadioButton,
                    name = "Al",
    
                };
    
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].UpdateLabels();
                }
    
    
    
                //dogs object
                dogs[0] = new Greyhound()
                {
                    myPictureBox = dogPictureBox1,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox1.Width,
                    Randomizer = myRandom
                };
    
                dogs[1] = new Greyhound()
                {
                    myPictureBox = dogPictureBox2,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox2.Width,
                    Randomizer = myRandom
                };
    
                dogs[2] = new Greyhound() 
                {
                    myPictureBox = dogPictureBox3,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox3.Width,
                    Randomizer = myRandom
                };
    
                dogs[3] = new Greyhound()
                {
                    myPictureBox = dogPictureBox4,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox4.Width,
                    Randomizer = myRandom
                };
    
    
            }
    
            private void betButton_Click(object sender, EventArgs e)
            {
    
                if (sender == alRadioButton)
                {
    
                    if (!guys[2].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == joeRadioButton)
                {
                    if (!guys[0].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == bobRadioButton)
                {
                    if (!guys[1].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount!", "Unable to bet amount");
                    }
                }
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value);
                    guys[i].UpdateLabels();
                }
            }
    
            private void raceButton_Click(object sender, EventArgs e)
            {
    
                bool noWinner = true;
                int dogWon;
    
                while (noWinner)
                {
                    for (int i = 0; i < dogs.Length; i++)
                    {
                        if (dogs[i].Run())
                        {
                            dogWon = i + 1;
                        }
                    }
                }
            }
    
            private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[0].name;
            }
    
            private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[1].name;
            }
    
            private void alRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[2].name;
            }
        }
    };
    
    Guy.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace WindowsFormsApplication6
    {
        class Greyhound
        {
    
            public int StartingPosition; //Where my picturebox starts
            public int RaceTrackLength; //how long the racetrack is
            public PictureBox myPictureBox = null; //my picturebox object
            public int Location = 0; //My location on racetrack
            public Random Randomizer; //instance of Random
    
    
    
            public bool Run() //use randomizer like in the sub shop example
            {
                //Random Randomizer = new Random();
    
                int randomMovement = Randomizer.Next(1, 4);
    
                Point currentPos = myPictureBox.Location;
                currentPos.X += randomMovement;
                myPictureBox.Location = currentPos;
    
                        //Move forward either 1,2, 3, or 4 spaces at random
                        //update the position of my picture box on form
                        //return true if I won the race
    
                if (currentPos.X >= RaceTrackLength)
                {
                    return true;
                }
                else
                    return false;
    
            }
    
            public void TakeStartingPosition()
            {
                //reset location to the start line
                myPictureBox.Left = StartingPosition;
                Location = 0;
            }
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        class Guy
        {
    
            public string name; //the guy's name
            public Bet myBet = null; //an instance of Bet() that has his bet
            public int cash; //how much cash he has
    
            public RadioButton myRadioButton;
            public Label myLabel;
    
            public void UpdateLabels()
            {
                //set my label to my bet's description, and the label on my
                // radio button to show my cash ('Joe has 43 bucks')
    
                if (myBet == null)
                {
                    myLabel.Text = name + " hasn't placed a bet";
                }
                else {
                    myLabel.Text = myBet.GetDescription();
                    //Why isn't this working??
                }
    
                myRadioButton.Text = name + " has " + cash + " bucks";
    
            }
            public void ClearBet() 
            {
                //clear the betting table
                myBet.amount = 0;
            }
    
            public bool PlaceBet(int amount, int Dog)
            {
                Bet myBet = new Bet();
                    myBet.Dog = Dog;
                    myBet.amount = amount;
                    myBet.Bettor = this;
                    UpdateLabels();
    
    
    
                //place a new bet and store it in my bet field
                //return true if the guy had enough money to bet
    
                if (amount >= cash)
                {
                    return false;
                }
                else
                    return true;
            }
    
            public void Collect(int Winner) {
                cash += myBet.PayOut(Winner);
            } //ask my bet to pay out
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication6
    {
        class Bet
        {
            public int amount; //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 Bettor.name + " bets " + amount + "on dog #" + Dog;
                }
                else
                    return Bettor.name + " has not placed a bet!";
            }
    
            public int PayOut(int Winner)
            {
    
                //the parameter is the winner of the race. if the dog won, 
                //returns 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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
    
            //initialize dog and guy arrays
            Greyhound[] dogs = new Greyhound[4];
            Guy[] guys = new Guy[3];
            Random myRandom = new Random();
    
    
            public Form1()
            {
                InitializeComponent();
    
                //guys object initialization
                guys[0] = new Guy()
                {
                    cash = 50,
                    myLabel = joeBetLabel,
                    myRadioButton = joeRadioButton,
                    name = "Joe"
    
                };
    
                guys[1] = new Guy()
                {
                    cash = 75,
                    myLabel = bobBetLabel,
                    myRadioButton = bobRadioButton,
                    name = "Bob"
                };
    
                guys[2] = new Guy()
                {
                    cash = 45,
                    myLabel = alBetLabel,
                    myRadioButton = alRadioButton,
                    name = "Al",
    
                };
    
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].UpdateLabels();
                }
    
    
    
                //dogs object
                dogs[0] = new Greyhound()
                {
                    myPictureBox = dogPictureBox1,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox1.Width,
                    Randomizer = myRandom
                };
    
                dogs[1] = new Greyhound()
                {
                    myPictureBox = dogPictureBox2,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox2.Width,
                    Randomizer = myRandom
                };
    
                dogs[2] = new Greyhound() 
                {
                    myPictureBox = dogPictureBox3,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox3.Width,
                    Randomizer = myRandom
                };
    
                dogs[3] = new Greyhound()
                {
                    myPictureBox = dogPictureBox4,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox4.Width,
                    Randomizer = myRandom
                };
    
    
            }
    
            private void betButton_Click(object sender, EventArgs e)
            {
    
                if (sender == alRadioButton)
                {
    
                    if (!guys[2].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == joeRadioButton)
                {
                    if (!guys[0].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == bobRadioButton)
                {
                    if (!guys[1].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount!", "Unable to bet amount");
                    }
                }
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value);
                    guys[i].UpdateLabels();
                }
            }
    
            private void raceButton_Click(object sender, EventArgs e)
            {
    
                bool noWinner = true;
                int dogWon;
    
                while (noWinner)
                {
                    for (int i = 0; i < dogs.Length; i++)
                    {
                        if (dogs[i].Run())
                        {
                            dogWon = i + 1;
                        }
                    }
                }
            }
    
            private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[0].name;
            }
    
            private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[1].name;
            }
    
            private void alRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[2].name;
            }
        }
    };
    
    Bet.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace WindowsFormsApplication6
    {
        class Greyhound
        {
    
            public int StartingPosition; //Where my picturebox starts
            public int RaceTrackLength; //how long the racetrack is
            public PictureBox myPictureBox = null; //my picturebox object
            public int Location = 0; //My location on racetrack
            public Random Randomizer; //instance of Random
    
    
    
            public bool Run() //use randomizer like in the sub shop example
            {
                //Random Randomizer = new Random();
    
                int randomMovement = Randomizer.Next(1, 4);
    
                Point currentPos = myPictureBox.Location;
                currentPos.X += randomMovement;
                myPictureBox.Location = currentPos;
    
                        //Move forward either 1,2, 3, or 4 spaces at random
                        //update the position of my picture box on form
                        //return true if I won the race
    
                if (currentPos.X >= RaceTrackLength)
                {
                    return true;
                }
                else
                    return false;
    
            }
    
            public void TakeStartingPosition()
            {
                //reset location to the start line
                myPictureBox.Left = StartingPosition;
                Location = 0;
            }
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        class Guy
        {
    
            public string name; //the guy's name
            public Bet myBet = null; //an instance of Bet() that has his bet
            public int cash; //how much cash he has
    
            public RadioButton myRadioButton;
            public Label myLabel;
    
            public void UpdateLabels()
            {
                //set my label to my bet's description, and the label on my
                // radio button to show my cash ('Joe has 43 bucks')
    
                if (myBet == null)
                {
                    myLabel.Text = name + " hasn't placed a bet";
                }
                else {
                    myLabel.Text = myBet.GetDescription();
                    //Why isn't this working??
                }
    
                myRadioButton.Text = name + " has " + cash + " bucks";
    
            }
            public void ClearBet() 
            {
                //clear the betting table
                myBet.amount = 0;
            }
    
            public bool PlaceBet(int amount, int Dog)
            {
                Bet myBet = new Bet();
                    myBet.Dog = Dog;
                    myBet.amount = amount;
                    myBet.Bettor = this;
                    UpdateLabels();
    
    
    
                //place a new bet and store it in my bet field
                //return true if the guy had enough money to bet
    
                if (amount >= cash)
                {
                    return false;
                }
                else
                    return true;
            }
    
            public void Collect(int Winner) {
                cash += myBet.PayOut(Winner);
            } //ask my bet to pay out
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication6
    {
        class Bet
        {
            public int amount; //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 Bettor.name + " bets " + amount + "on dog #" + Dog;
                }
                else
                    return Bettor.name + " has not placed a bet!";
            }
    
            public int PayOut(int Winner)
            {
    
                //the parameter is the winner of the race. if the dog won, 
                //returns 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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
    
            //initialize dog and guy arrays
            Greyhound[] dogs = new Greyhound[4];
            Guy[] guys = new Guy[3];
            Random myRandom = new Random();
    
    
            public Form1()
            {
                InitializeComponent();
    
                //guys object initialization
                guys[0] = new Guy()
                {
                    cash = 50,
                    myLabel = joeBetLabel,
                    myRadioButton = joeRadioButton,
                    name = "Joe"
    
                };
    
                guys[1] = new Guy()
                {
                    cash = 75,
                    myLabel = bobBetLabel,
                    myRadioButton = bobRadioButton,
                    name = "Bob"
                };
    
                guys[2] = new Guy()
                {
                    cash = 45,
                    myLabel = alBetLabel,
                    myRadioButton = alRadioButton,
                    name = "Al",
    
                };
    
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].UpdateLabels();
                }
    
    
    
                //dogs object
                dogs[0] = new Greyhound()
                {
                    myPictureBox = dogPictureBox1,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox1.Width,
                    Randomizer = myRandom
                };
    
                dogs[1] = new Greyhound()
                {
                    myPictureBox = dogPictureBox2,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox2.Width,
                    Randomizer = myRandom
                };
    
                dogs[2] = new Greyhound() 
                {
                    myPictureBox = dogPictureBox3,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox3.Width,
                    Randomizer = myRandom
                };
    
                dogs[3] = new Greyhound()
                {
                    myPictureBox = dogPictureBox4,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox4.Width,
                    Randomizer = myRandom
                };
    
    
            }
    
            private void betButton_Click(object sender, EventArgs e)
            {
    
                if (sender == alRadioButton)
                {
    
                    if (!guys[2].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == joeRadioButton)
                {
                    if (!guys[0].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == bobRadioButton)
                {
                    if (!guys[1].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount!", "Unable to bet amount");
                    }
                }
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value);
                    guys[i].UpdateLabels();
                }
            }
    
            private void raceButton_Click(object sender, EventArgs e)
            {
    
                bool noWinner = true;
                int dogWon;
    
                while (noWinner)
                {
                    for (int i = 0; i < dogs.Length; i++)
                    {
                        if (dogs[i].Run())
                        {
                            dogWon = i + 1;
                        }
                    }
                }
            }
    
            private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[0].name;
            }
    
            private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[1].name;
            }
    
            private void alRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[2].name;
            }
        }
    };
    
    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace WindowsFormsApplication6
    {
        class Greyhound
        {
    
            public int StartingPosition; //Where my picturebox starts
            public int RaceTrackLength; //how long the racetrack is
            public PictureBox myPictureBox = null; //my picturebox object
            public int Location = 0; //My location on racetrack
            public Random Randomizer; //instance of Random
    
    
    
            public bool Run() //use randomizer like in the sub shop example
            {
                //Random Randomizer = new Random();
    
                int randomMovement = Randomizer.Next(1, 4);
    
                Point currentPos = myPictureBox.Location;
                currentPos.X += randomMovement;
                myPictureBox.Location = currentPos;
    
                        //Move forward either 1,2, 3, or 4 spaces at random
                        //update the position of my picture box on form
                        //return true if I won the race
    
                if (currentPos.X >= RaceTrackLength)
                {
                    return true;
                }
                else
                    return false;
    
            }
    
            public void TakeStartingPosition()
            {
                //reset location to the start line
                myPictureBox.Left = StartingPosition;
                Location = 0;
            }
    
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        class Guy
        {
    
            public string name; //the guy's name
            public Bet myBet = null; //an instance of Bet() that has his bet
            public int cash; //how much cash he has
    
            public RadioButton myRadioButton;
            public Label myLabel;
    
            public void UpdateLabels()
            {
                //set my label to my bet's description, and the label on my
                // radio button to show my cash ('Joe has 43 bucks')
    
                if (myBet == null)
                {
                    myLabel.Text = name + " hasn't placed a bet";
                }
                else {
                    myLabel.Text = myBet.GetDescription();
                    //Why isn't this working??
                }
    
                myRadioButton.Text = name + " has " + cash + " bucks";
    
            }
            public void ClearBet() 
            {
                //clear the betting table
                myBet.amount = 0;
            }
    
            public bool PlaceBet(int amount, int Dog)
            {
                Bet myBet = new Bet();
                    myBet.Dog = Dog;
                    myBet.amount = amount;
                    myBet.Bettor = this;
                    UpdateLabels();
    
    
    
                //place a new bet and store it in my bet field
                //return true if the guy had enough money to bet
    
                if (amount >= cash)
                {
                    return false;
                }
                else
                    return true;
            }
    
            public void Collect(int Winner) {
                cash += myBet.PayOut(Winner);
            } //ask my bet to pay out
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication6
    {
        class Bet
        {
            public int amount; //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 Bettor.name + " bets " + amount + "on dog #" + Dog;
                }
                else
                    return Bettor.name + " has not placed a bet!";
            }
    
            public int PayOut(int Winner)
            {
    
                //the parameter is the winner of the race. if the dog won, 
                //returns 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.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
    
            //initialize dog and guy arrays
            Greyhound[] dogs = new Greyhound[4];
            Guy[] guys = new Guy[3];
            Random myRandom = new Random();
    
    
            public Form1()
            {
                InitializeComponent();
    
                //guys object initialization
                guys[0] = new Guy()
                {
                    cash = 50,
                    myLabel = joeBetLabel,
                    myRadioButton = joeRadioButton,
                    name = "Joe"
    
                };
    
                guys[1] = new Guy()
                {
                    cash = 75,
                    myLabel = bobBetLabel,
                    myRadioButton = bobRadioButton,
                    name = "Bob"
                };
    
                guys[2] = new Guy()
                {
                    cash = 45,
                    myLabel = alBetLabel,
                    myRadioButton = alRadioButton,
                    name = "Al",
    
                };
    
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].UpdateLabels();
                }
    
    
    
                //dogs object
                dogs[0] = new Greyhound()
                {
                    myPictureBox = dogPictureBox1,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox1.Width,
                    Randomizer = myRandom
                };
    
                dogs[1] = new Greyhound()
                {
                    myPictureBox = dogPictureBox2,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox2.Width,
                    Randomizer = myRandom
                };
    
                dogs[2] = new Greyhound() 
                {
                    myPictureBox = dogPictureBox3,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox3.Width,
                    Randomizer = myRandom
                };
    
                dogs[3] = new Greyhound()
                {
                    myPictureBox = dogPictureBox4,
                    StartingPosition = raceTrackPictureBox.Left,
                    RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox4.Width,
                    Randomizer = myRandom
                };
    
    
            }
    
            private void betButton_Click(object sender, EventArgs e)
            {
    
                if (sender == alRadioButton)
                {
    
                    if (!guys[2].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == joeRadioButton)
                {
                    if (!guys[0].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                    }
                }
                else if (sender == bobRadioButton)
                {
                    if (!guys[1].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                    {
                        //display message saying you can't bet that amount  
                        MessageBox.Show("You can not bet this amount!", "Unable to bet amount");
                    }
                }
                for (int i = 0; i < guys.Length; i++)
                {
                    guys[i].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value);
                    guys[i].UpdateLabels();
                }
            }
    
            private void raceButton_Click(object sender, EventArgs e)
            {
    
                bool noWinner = true;
                int dogWon;
    
                while (noWinner)
                {
                    for (int i = 0; i < dogs.Length; i++)
                    {
                        if (dogs[i].Run())
                        {
                            dogWon = i + 1;
                        }
                    }
                }
            }
    
            private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[0].name;
            }
    
            private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[1].name;
            }
    
            private void alRadioButton_CheckedChanged(object sender, EventArgs e)
            {
                label2.Text = guys[2].name;
            }
        }
    };
    
    使用系统;
    使用System.Collections.Generic;
    使用系统组件模型;
    使用系统数据;
    使用系统图;
    使用System.Linq;
    使用系统文本;
    使用System.Windows.Forms;
    命名空间Windows窗体应用程序6
    {
    公共部分类Form1:Form
    {
    //初始化dog和guy数组
    灰狗[]狗=新灰狗[4];
    Guy[]guys=新人[3];
    Random myRandom=新的Random();
    公共表格1()
    {
    初始化组件();
    //对象初始化
    伙计们[0]=新伙计们()
    {
    现金=50,
    myLabel=joeBetLabel,
    myRadioButton=joeRadioButton,
    name=“乔”
    };
    小伙子们[1]=新来的小伙子们()
    {
    现金=75,
    myLabel=bobBetLabel,
    myRadioButton=bobRadioButton,
    name=“鲍勃”
    };
    小伙子们,小伙子们
    {
    现金=45,
    myLabel=alBetLabel,
    myRadioButton=alRadioButton,
    name=“Al”,
    };
    for(int i=0;i
    编辑 在你的帮助下,我似乎已经找到了解决这两个问题的办法,但现在我似乎无法让比赛停止。他们只是不断地绕到起点,而不是结束比赛。在过去的一个小时里,我一直在玩它,并改变了比赛按钮,但没有成功。有什么帮助吗

        private void raceButton_Click(object sender, EventArgs e)
        {
    
            bool noWinner = false;
            int dogWon;
    
            while (!noWinner)
            {
               Application.DoEvents();
    
                for (int i = 0; i < dogs.Length; i++)
                {
                    if (dogs[i].Run())
                    {
                        dogWon = i +1;
                        MessageBox.Show("The Winner is dog #" + dogWon, "Winner!");
                        for (int j = 0; j < dogs.Length; j++)
                        {
                            dogs[j].TakeStartingPosition();
                        }
                    }
                }
            }
        }
    
    private void raceButton\u单击(对象发送者,事件参数e)
    {
    bool nowniner=false;
    多格旺酒店