c# - 使用 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);
}
}
}
解决方案
value
在 setter 内部使用
public string Biome {
get {
return biome;
}
set {
if (value == "Temperate" ||
value == "Tropical" ||
value == "Boreal") {
biome = value;
} else {
biome = "Unknown";
}
}
}
推荐阅读
- unity3d - 按钮更改面板
- asp.net-core - 重定向到.net核心中的查询字符串url时会话为空
- python - 从多行文件中获取特定文本
- java - 将数据从第三个活动传输到第一个活动
- linux - 尝试在 Ubuntu 18、Linux 上安装 QT 包时出现 QXcbConnection 错误
- c# - 使用 /p:PublishSingleFile=true 标志发布时出错
- ssl - 如何为 confluent kafka python 客户端配置 ssl 证书和密钥?
- c# - C# 正则表达式不匹配
- sql - 如何将表与自身连接以获取数据的层次结构
- c# - Sikuli 可以在用 C# 编写的 Selenium 脚本中使用吗