我目前正在处理一本书为我分配的项目(Head First C#,3rd Edition),我的代码遇到了一些问题。
整个项目基于3个人可以参与的赌博游戏。
赌博游戏包括投注4只狗的3个人,可以在赛道上随机赢得比赛。
其中一个问题是当一个人投入的金额比他更多时,该计划将金额的值设为0.
我不知道为什么我没有看到明显的错误,但如果有人愿意,我真的很感激帮助。
整个计划的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace A_Day_at_the_Races
{
public class Guy
{
public string Name; // The guy's name
public Bet MyBet = null; // An instance of Bet() that has how much he's betting
public int Cash; // How much cash he has
// These 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()
{
//1.Set my label to my bet's description,
if (MyBet == null)
MyLabel.Text = Name + " hasnt placed any bets";
else if (MyBet.Amount > Cash)
MyLabel.Text = Name + " doesn't have that amount of money";
else
MyLabel.Text = MyBet.GetDescription();
if (Cash >= 0)
{
//2.Set the label on my radio button to show my cash ("Joe has 43 dollars")
MyRadioButton.Text = Name + " has " + Cash + " bucks";
}
else
{
MyRadioButton.Text = Name + " has no cash left";
}
}
public void ClearBet()
{
//1.Reset my bet so it's zero
MyBet = null;
}
public bool PlaceBet(int Amount, int Dog)
{
//1.Place a new bet and store it in my bet field
this.MyBet = new Bet();
//2.Return true if the guy had enough money to bet
if (Cash >= Amount)
{
Cash = Cash - Amount; // Remove the amount bet
MyBet.Amount = Amount;
MyBet.Dog = Dog;
MyBet.Bettor = this;
UpdateLabels();
return true;
}
else if (Cash < Amount)
{
return false;
}
else
{
return false;
}
}
public void Collect(int Winner)
{
if (MyBet != null)
//1.Ask my bet to pay out (hint use the bet object to do the work)
Cash += MyBet.PayOut(Winner);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace A_Day_at_the_Races
{
public class Greyhound
{
public int StartingPosition; // Where PictureBox starts
public int RacetrackLength; // 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 random
int x = Randomizer.Next(1, 4);
// Update the position of my PictureBox on the form
Point p = MyPictureBox.Location;
p.X += x;
MyPictureBox.Location = p;
// Return true if I won the game
if (p.X >= RacetrackLength)
{
return true;
}
else
{
return false;
}
}
public void TakeStartingPosition ()
{
// Reset starting position of PictureBox
StartingPosition = 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A_Day_at_the_Races
{
public 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 = new Guy(); // 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 (Amount > 0 && Amount < Bettor.Cash)
{
return Bettor.Name + " bets $" + Amount + " on dog #" + Dog;
}
// If the amount is zero, no bet was placed; ("Joe hasn't placed
// a bet").
else if (Amount == 0)
{
return Bettor.Name + " hasn't placed a bet";
}
else if (Amount > Bettor.Cash)
{
return Bettor.Name + " doesn't have that amount of money";
}
else
{
return Bettor.Name + "'s bet";
}
}
public int PayOut(int Winner) // The parameter is the winner of the race
{
// If the dog won, return the amount bet.
if (Winner == Dog)
{
return Amount;
}
// Otherwise, return the negative of the amount bet.
else
{
return -1 * 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 A_Day_at_the_Races
{
public partial class Form1 : Form
{
Guy[] Bettors;
Greyhound[] Dogs;
Guy CurrentBettor;
public Form1()
{
InitializeComponent();
Random Randomizer = new Random();
// Initialise all my Guys and Dogs
Bettors = new Guy[3];
Dogs = new Greyhound[4];
// Guys
// Joe
Bettors[0] = new Guy();
Bettors[0].Name = "Joe";
Bettors[0].MyRadioButton = joeRadioButton;
Bettors[0].MyLabel = joeBetLabel;
Bettors[0].Cash = 50;
Bettors[0].UpdateLabels();
// Bob
Bettors[1] = new Guy();
Bettors[1].Name = "Bob";
Bettors[1].MyRadioButton = bobRadioButton;
Bettors[1].MyLabel = bobBetLabel;
Bettors[1].Cash = 75;
Bettors[1].UpdateLabels();
// Al
Bettors[2] = new Guy();
Bettors[2].Name = "Al";
Bettors[2].MyRadioButton = alRadioButton;
Bettors[2].MyLabel = alBetLabel;
Bettors[2].Cash = 45;
Bettors[2].UpdateLabels();
// Local integers on distance and starting position of the Dogs
int StartPosition = pictureBoxDog1.Location.X;
int distance = pictureBox1.Width;
// Initialize all 4 Dogs
for (int i = 0; i < Dogs.Length; i++)
{
Dogs[i] = new Greyhound();
Dogs[i].Randomizer = Randomizer;
Dogs[i].RacetrackLength = distance;
Dogs[i].Location = Dogs[i].StartingPosition = StartPosition;
}
// Connect pictureboxes with MyPictureBox in the Dog class
Dogs[0].MyPictureBox = pictureBoxDog1;
Dogs[1].MyPictureBox = pictureBoxDog2;
Dogs[2].MyPictureBox = pictureBoxDog3;
Dogs[3].MyPictureBox = pictureBoxDog4;
CurrentBettor = Bettors[0];
}
private void RaceButton_Click(object sender, EventArgs e)
{
int winner = 0; // Number of the winner Dog
int num_winners = 0; // Amount of winners
// While none has won,
while (num_winners == 0)
{
for (int i = 0; i < Dogs.Length; i++)
{
// If a Dog has reached max length of the race track,
// increment number of winners and add 1 to i and save it
// to the winner variable
if (Dogs[i].Run())
{
num_winners++;
winner = i + 1;
}
}
Application.DoEvents();
System.Threading.Thread.Sleep(3);
}
// If there is more than one winner, show this message
if (num_winners > 1)
MessageBox.Show("We have " + num_winners + " winners");
// Otherwise if there are more than or equal to one winner,
// show this message
else if (num_winners >= 1)
MessageBox.Show("Dog #" + winner + " wins!");
// Place all Dogs in their starting positions
for (int i = 0; i < Dogs.Length; i++)
{
Dogs[i].TakeStartingPosition();
}
for (int i = 0; i < Bettors.Length; i++)
{
Bettors[i].Collect(winner);
Bettors[i].ClearBet();
Bettors[i].UpdateLabels();
}
numericUpDownBet.Value = numericUpDownBet.Minimum;
numericUpDownDog.Value = numericUpDownDog.Minimum;
}
private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
{
// Bettor = Joe when his radio button is checked
SetBettor(0);
}
private void bobRadioButton_CheckedChanged_1(object sender, EventArgs e)
{
// Bettor = Bob when his radio button is checked
SetBettor(1);
}
private void alRadioButton_CheckedChanged_1(object sender, EventArgs e)
{
// Bettor = Al when his radio button is checked
SetBettor(2);
}
private void BetsButton_Click(object sender, EventArgs e)
{
// Place bet depending on the value of the two numericUpDowns
// (Amount & Dog)
CurrentBettor.PlaceBet((int)numericUpDownBet.Value, (int)numericUpDownDog.Value);
CurrentBettor.UpdateLabels();
}
private void SetBettor(int index)
{
// Set the current bettor and update the name label
// to the his name
CurrentBettor = Bettors[index];
NameLabel.Text = CurrentBettor.Name;
// If the Guy's bet isn't 0,
// then save the amount and the dog he's
// betting on
if (CurrentBettor.MyBet != null)
{
numericUpDownBet.Value = CurrentBettor.MyBet.Amount;
numericUpDownDog.Value = CurrentBettor.MyBet.Dog;
}
// Otherwise, make the value of the bet
// to the minimum on the numericUpDownBet
// and set the default Dog that is being
// bet on as the first Dog
else
{
numericUpDownBet.Value = numericUpDownBet.Minimum;
numericUpDownDog.Value = 1;
}
}
private void Form1_Load(object sender, EventArgs e)
{
minimumBetLabel.Text = "Minimum Bet: $5.00";
}
private void ResetButton_Click(object sender, EventArgs e)
{
// Reset Dogs to their default location when
// the Reset button is pressed
pictureBoxDog1.Location = new Point(22, 21);
pictureBoxDog2.Location = new Point(22, 63);
pictureBoxDog3.Location = new Point(22, 120);
pictureBoxDog4.Location = new Point(22, 170);
}
}
}
感谢所有答案。
答案 0 :(得分:2)
问题出在以下代码中。
在这里,创建了一个新的赌注(Amount
默认为0,否则设置为!):
//1.Place a new bet and store it in my bet field
this.MyBet = new Bet();
此处,所有相关数据均设为MyBet
:
//2.Return true if the guy had enough money to bet
if (Cash >= Amount)
{
Cash = Cash - Amount; // Remove the amount bet
MyBet.Amount = Amount;
MyBet.Dog = Dog;
MyBet.Bettor = this;
UpdateLabels();
return true;
}
此处,没有为MyBet
设置数据,因此Amount
仍为0:
else if (Cash < Amount)
{
return false;
}
这根本不会发生:
else
{
return false;
}
答案 1 :(得分:0)
我PlaceBet - 你创建一个空的Bet,然后检查,如果这个人有足够的现金。如果他没有,那么空投注将保持为空 - 其金额字段将保持默认值0。
public bool PlaceBet(int Amount, int Dog)
{
//1.Place a new bet and store it in my bet field
this.MyBet = new Bet(); // EMPTY BET
//2.Return true if the guy had enough money to bet
if (Cash >= Amount)
{
Cash = Cash - Amount; // Remove the amount bet
MyBet.Amount = Amount;
MyBet.Dog = Dog;
MyBet.Bettor = this;
UpdateLabels();
return true;
}
else if (Cash < Amount)
{
// THE BET REMAINS EMPTY
return false;
}
else
{
return false;
}
}
此外,&#34;其他&#34;块永远不会发生 - 您实际上可以删除else if (Cash < Amount)
案例。
答案 2 :(得分:0)
//1.Place a new bet and store it in my bet field
this.MyBet = new Bet();
//2.Return true if the guy had enough money to bet
if (Cash >= Amount)
{
Cash = Cash - Amount; // Remove the amount bet
MyBet.Amount = Amount;
MyBet.Dog = Dog;
MyBet.Bettor = this;
UpdateLabels();
return true;
}
else if (Cash < Amount)
{
return false;
}
在(1)你正在创建一个新的Bet对象然后如果Cash&lt;数量为(2)你返回false而不设置this.MyBet为null。
最好的解决方案可能就是把这个.MyBet = new Bet();现金&gt; =金额块。