好的,我正在加入我的上一个项目,在那里我创建了一个鲨鱼比赛。一切都顺利进行,然后才变得很花哨。
以下是要求: 1)将所有实例变量声明为使用get和set定义的公共属性 存取方法以及相应的私有支持字段。 2)从Guy类中删除MyBet实例变量,而不是提供 Form1类中包含每个Bet实例的数组。下注客厅应该 现在允许每个盖子下注多次。但是,每次下注的金额 应该在投注时从每个盖伊的现金财产中扣除。 因此,奖金只是在比赛后支付的正数量。 3)有一个记分板(或某种类型的视觉显示),指示其顺序 每个赛车手完成,允许有第一,第二或第三的关系 地方。
我得到大约50个相同的错误,似乎它们都是非常快速的修复,但由于某种原因我无法弄清楚它们。提前谢谢!
考虑到这一点,这是我的表格:
表格类:
using System;
使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Linq; 使用System.Text; 使用System.Windows.Forms;
命名空间project1 { 公共部分类游戏:表格 { 私人鲨鱼[]鲨鱼; 私人盖伊[]伙计们; 私人Guy selectedGuy; 私人投注[]投注; private int [] winners = new int [4];
public Game()
{
InitializeComponent();
Random moreRandom = new Random();
int start = myTrack.Location.X;
int finish = myTrack.Width - 65;
sharks = new Shark[4]
{
new Shark() {myRandom = moreRandom, myPictureBox = myShark1, myPBStart = start, trackLength = finish},
new Shark() {myRandom = moreRandom, myPictureBox = myShark2, myPBStart = start, trackLength = finish},
new Shark() {myRandom = moreRandom, myPictureBox = myShark3, myPBStart = start, trackLength = finish},
new Shark() {myRandom = moreRandom, myPictureBox = myShark4, myPBStart = start, trackLength = finish}
};
guys = new Guy[3]
{
new Guy() {Name="Joe", Moneys=50, MyRB=rbGuy1, GuyLabel=labelBet1},
new Guy() {Name="Bob", Moneys=75, MyRB=rbGuy2, GuyLabel=labelBet2},
new Guy() {Name="Al", Moneys=45, MyRB=rbGuy3, GuyLabel=labelBet3}
};
bets = new Bet[12];
for (int = 0; i<bets.Length; i++)
{
bets[i] = Bet.EmptyBet;
}
selectedGuy = guys[0];
rbGuy1.Tag = guys[0];
rbGuy2.Tag = guys[1];
rbGuy3.Tag = guys[2];
updateGui();
}
private void myChanged(object sender, EventArgs e)
{
selectedGuy = getSelectedGuy(sender);
betterLabel.Text = selectedGuy.Name;
}
private void betAmountValue(object sender, EventArgs e)
{
updateMin();
}
private void bet_Click(object sender, EventArgs e)
{
int bet = (int) betAmount.Value;
int myFish = (int) sharkNumber.Value;
Bet temp = selectedGuy.placeBet(bet,myFish);
if (!temp == Bet.EmptyBet)
{
for (int i = 0; i<3; i++)
{
if (guys[i].Name == selectedGuy.Name)
{
bets[i + (3 * myFish)] = temp;
}
}
/*for (int = 0; i<bets.Length, i++)
{
bets[i] = temp;
}*/
}
//selectedGuy.placeBet(bet, myFish);
updateGui();
}
private void raceBtn_Click(object sender, EventArgs e)
{
betBtn.Enabled = false;
Application.DoEvents();
//bool noWinner = true;
Random rand = new Random();
/*int winner = rand.Next(1,5);
showWinner(winner-1);
collectBets(winner-1);
*/
for(int i=0; i<4; i++)
winners[i] = 0;
for(int i=0; i<4; i++)
{
bool placed = false;
int temp;
while (!placed)
{
temp = rand.Next(0,4);
if (winners[temp] == 0)
{
winners[temp] = i;
placed = true;
}
}
}
showWinner(winners[0] - 1);
collectBets(winners[0] - 1);
/*while(noWinner)
{
for (int fish = 0; fish < sharks.Length; fish++)
{
Application.DoEvents();
if(sharks[fish].Swim())
{
showWinner(fish);
collectBets(fish);
noWinner = false;
}
}
}*/
updateGui();
betBtn.Enabled = true;
}
private void showWinner(int fish)
{
MessageBox.Show(string.Format("Winner Winner People Dinner! \nShark {0} won!", fish + 1));
}
private void collectBets(int fish)
{
for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
{
guys[guyNumber].collect(betTotal(), winningSharkTotal());
guys[guyNumber].resetBet();
}
}
private void updateMin()
{
minBetLabel.Text = string.Format("Minimum bet: 5 bucks", betAmount.Value);
}
private Guy getSelectedGuy(object sender)
{
RadioButton rb = (RadioButton)sender;
return (Guy)rb.Tag;
}
private void updateGui()
{
for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
{
guys[guyNumber].updateLabels();
}
for (int fish = 0; fish < sharks.Length; fish++)
{
sharks[fish].startPosition();
}
/*betTable1 = bets[0];
betTable2 = bets[1];
betTable3 = bets[2];*/
bet1_1.Text = string.Format(bets[0].Amount);
bet1_2.Text = bets[1].Amount;
bet1_3.Text = bets[2].Amount;
bet2_1.Text = bets[3].Amount;
bet2_2.Text = bets[4].Amount;
bet2_3.Text = bets[5].Amount;
bet3_1.Text = bets[6].Amount;
bet3_2.Text = bets[7].Amount;
bet3_3.Text = bets[8].Amount;
bet4_1.Text = bets[9].Amount;
bet4_2.Text = bets[10].Amount;
bet4_3.Text = bets[11].Amount;
updateMin();
firstWinner.Text = winners[0];
secondWinner.Text = winners[1];
thirdWinner.Text = winners[2];
fourthWinner.Text = winners[3];
}
private int betTotal()
{
int Result = 0;
for (int i=0; i<bets.Length; i++)
{
Result += bets[i].Amount;
}
return Result;
}
private int winningSharkTotal(int Winner)
{
int Result = 0;
for (int i = 0; i<3; i++)
{
Result += bets[i+(winner*3)];
}
return Result;
}
}
}
鲨鱼班:
using System;
使用System.Drawing; 使用System.Threading; 使用System.Windows.Forms;
命名空间project1 { 公共课鲨鱼 { public int myPBStart; // PictureBox的起始位置 public int trackLength; //赛道有多长 public PictureBox myPictureBox = null; // PictureBox对象 public int location = 0; //我在赛道上的位置 public随机myRandom; // Random
的实例 public Shark()
{
location = 0;
myPictureBox = new PictureBox();
myRandom = new Random();
trackLength = 100;
myPBStart = 0;
}
public bool Swim()
{
int distance = myRandom.Next(1, 4);
location += distance;
movePB(distance);
return location > trackLength;
}
private void movePB(int distance)
{
Point p = myPictureBox.Location;
p.X += distance;
myPictureBox.Location = p;
}
public void startPosition()
{
location = myPBStart;
Point p = myPictureBox.Location;
p.X = location;
myPictureBox.Location = p;
}
}
}
盖伊班:
using System.Windows.Forms;
命名空间project1 { 公共阶层盖伊 { 私有字符串myName; // public Bet myBet; 私人现金;
// The last two fields are the the guy's GUI controls on the form
private RadioButton myRadioButton;
private Label myLabel;
const int minBet = 5;
const int maxBet = 15;
public Guy()
{
//myBet = emptyBet();
//myBet = new Bet() { amount = 0, fish = 0, better = this };
}
public void updateLabels()
{
get{
// Set my label to my bet's description and its label
myLabel.Text = myBet.Description();
// cash radio buttons
myRadioButton.Text = radioButtonText();
}
}
public void resetBet()
{
// reset my bet to 0
myBet = Bet.EmptyBet;
}
public string Name{
get{
return myName;
}
}
public int Moneys
{
get
{
return cash;
}
}
public RadioButton MyRB
{
get
{
return myRadioButton;
}
}
public Label GuyLabel
{
get
{
return myLabel;
}
}
public Bet placeBet(int amount, int fish)
{
if (amount > cash || amount < minBet || amount > maxBet)
{
return Bet.EmptyBet;
//return false;
}
cash -= amount;
return new Bet() {Amount=amount, Fish=fish, better=this};
//return true;
}
public void collect(int pool, int sharkPool)
{
get{
return cash += myBet.payout(pool, sharkPool);
}
}
private string radioButtonText()
{
return string.Format("{0} has {1} bucks", myName, cash);
}
}
}
投注类:
namespace project1
{ 公共课Bet { 私人金额; //投注的现金金额 私人鱼类; //投注的鲨鱼数量。 公众盖伊更好; //下注的人
public static Bet EmptyBet{
get
{
return new Bet() { amount = 0, fish = 0, better = this };
}
}
public string Description
{
get
{
if (noBet())
{
return string.Format("{0} hasn't placed a bet", better.Name);
}
else
{
return string.Format("{0} bets {1} bucks on shark #{2}", better.Name, amount, fish);
}
}
}
public int payout(int pool, int sharkPool)
{
get{
return ((pool * (0.85))/sharkPool) * amount;
/*if (myShark(pool))
{
return amount;
}
else
{
return -amount;
}*/
}
}
/*private bool myShark(int winner)
{
return winner == fish;
}*/
private bool noBet()
{
return amount == 0;
}
public int Amount
{
get
{
return amount;
}
}
public int Fish
{
get
{
return fish;
}
}
}
}
答案 0 :(得分:5)
您混淆了方法和属性的概念。首先从您的方法中删除get { }
,然后考虑void
是否为正确的返回类型。从它的外观来看,你会返回某种decimal
值。
public void collect(int pool, int sharkPool)
{
get{
return cash += myBet.payout(pool, sharkPool);
}
}
在以下代码中,通过包含文字0.85
,结果值将具有类型double
。方法的返回类型是int
。
public int payout(int pool, int sharkPool)
{
get{
return ((pool * (0.85))/sharkPool) * amount;
/*if (myShark(pool))
{
return amount;
}
else
{
return -amount;
}*/
}
}
正如我之前提到的,删除get
并将返回类型标记为double
。
public double payout(int pool, int sharkPool)
{
return ((pool * (0.85))/sharkPool) * amount;
}
还有更多,但我认为你有更大的鱼可以炒。我会回过头来回顾一下C#语法的基础知识,并尝试更好地理解数值数据类型。
使用货币时,您确实需要使用decimal
。
答案 1 :(得分:2)
混乱是正确的(并且是看到我忽略的东西的道具)。看起来还有一些类型转换错误是通过将Bet.Amount分配给TextBox而生成的。在整数值上调用ToString(),这些错误应该消失。
错误42无法隐式转换 输入'int'到'string'
请请记住,UI元素(如标签,单选按钮,图片框)不属于Guys或Sharks等业务对象。这是设计决策,它比修复一些编译器错误更为重要(尽管我确定目前看来并不是这样)。