简体   繁体   English

当int超出我的else-if语句的边界时,我的设置器不会返回0

[英]My setter won't return 0 when an int is out of bounds of my else-if statement

My constructors are supposed to return the score given as long as the score is from 0 to 300. If the score is outside this boundary, a value of 0 should be returned. 只要分数在0到300之间,我的构造函数都应返回给定的分数。如果分数在此范围之外,则应返回0值。 However, it instead returns the value of what i gave my class, rather than what I set. 但是,它返回的是我给我的课程的价值,而不是我设置的价值。 Main Program 主程序

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);

         }
     }
 }

Class

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.");
         }
     }
 }

You're not range-checking value , which is the new value. 您不是范围检查value ,这是新值。 You're range-checking Score -- the old value. 您正在检查Score - 值。 This is what you intended to do: 这是您打算要做的:

     set
     {
         if (value >= 0 && value <= 300)
         {
             this.score = value;
         }
         else
         {
             this.score = 0;
         }
     }

I'm guessing this was just absent-mindedness. 我猜这只是心不在b。

UPDATE 更新

You could do the same thing less verbosely: 您可以不太冗长地执行相同的操作:

     set
     {
         this.score = (value >= 0 && value <= 300)
                          ? value
                          : 0;
     }

...but if that looks like line noise to you, stick with what you've got! ...但是,如果这看起来像是您的线路噪音,请坚持使用您所拥有的!

Another thing I'd suggest is renaming score to _score . 我建议的另一件事是将score重命名为_score That's the C# convention for private fields, and it prevents you from ever accidentally setting score when you really wanted to set Score . 这对私人领域的C#约定,它阻止你曾经不小心设置score ,当你真的想设定Score

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM