【发布时间】:2018-03-28 06:13:37
【问题描述】:
只要分数在 0 到 300 之间,我的构造函数就应该返回给定的分数。如果分数超出此边界,则应返回 0 值。但是,它返回的是我给我的班级的值,而不是我设置的值。 主程序
namespace ClassScores
{
class Program
{
static void Main(string[] args)
{
int runningTotal = 0;
double average = 0;
.....
Bowler Jesus = new Bowler("Jesus", 450);
bowlers[3] = Jesus;
for (int i=0; i <= 4; i++)
{
runningTotal=runningTotal + bowlers[i].Score;
}
average = Convert.ToDouble(runningTotal/5);
Console.WriteLine("The average bowler score is " + average);
}
}
}
类
namespace ClassScores
{
class Bowler
{
private string name;
private int score;
public string Name
{
.....
}
public int Score
{
get
{
return this.score;
}
set
{
if (Score>=0 && Score <=300)
{
this.score = value;
}
else
{
this.score = 0;
}
}
}
public Bowler (string name, int score)
{
this.Name = name;
this.Score = score;
}
public string ToString()
{
return (Name + " has a score of " + Convert.ToString(Score) + " points.");
}
}
}
【问题讨论】:
标签: c# arrays class object constructor