首页 > 解决方案 > 使用 Codecademy 学习,无法解决此错误

问题描述

我正在学习使用 Codecademy 进行编码,但我无法弄清楚这一点。它说“生物群系应该接受热带作为一种价值”。它没有给我语法错误。我在这里先向您的帮助表示感谢!

//Codecademy的实际问题/说明!

“为 biome 字段定义一个 Biome 属性。它将有一个 getter 和 setter。setter 应该只允许三个值:“Tropical”、“Temperate”和“Boreal”。如果使用任何其他值,请将 biome 设置为“未知”。

         //THIS IS THE FOREST CLASS!
    {
              class Forest
              {
                public string name;
                public int trees;
                public int age;
                public string biome;
                
                public string Name
                {
                  get {return name;}
                  set
                  {
                   name = Name;
                  }
                }
                 public int Trees
                 {
                    get {return trees;}
                    set
                    {
                      trees = Trees;
            
                    }
            
                 }
            
                 public string Biome{
                   get {return biome;}
                   set {
                     if (Biome == "Temperate" ||
                         Biome == "Tropical" ||
                         Biome == "Boreal"){
                           biome = Biome;
                         }
                      else 
                      {
                        biome = null;
                      }
            
                       }
                   }
            
            
                
              }


   //THIS IS THE PROGRAM CLASS!

     using System;
    
    namespace BasicClasses
    {
      class Program
      {
        static void Main(string[] args)
        {
          Forest f = new Forest();
          f.Name = "Congo";
          f.Trees = 0;
          f.age = 0;
          f.Biome = "Tropical";
          
          Console.WriteLine(f.Name);
          Console.WriteLine(f.Biome);
        }
      }
    }

标签: c#

解决方案


value在 setter 内部使用

public string Biome {
  get {
    return biome;
  }
  set {
    if (value == "Temperate" ||
      value == "Tropical" ||
      value == "Boreal") {
      biome = value;
    } else {
      biome = "Unknown";
    }
  }
}

推荐阅读