得到一个我不理解的非常奇怪的逻辑错误

时间:2016-08-15 09:21:27

标签: c#

此代码应该输出说

的行

trex(2腿)恐龙(灭绝)

获取从文件中获取的动物列表。 如果动物没有灭绝,则会被if语句跳过。

一切正常,除了最后一点,应该说(灭绝)。

相反,它说"是"或"假"取决于我如何设置isExtinct属性。

现在我已将if语句放入isExtinct属性中以输出" extinct"但我在编译后得到错误

Stacktrace:


Native stacktrace:

mono() [0x8105b4a]
mono() [0x8153274]
mono() [0x806a20b]
[0x777b940c]
mono() [0x8153224]
mono() [0x806a20b]
[0x777b940c]
[0x7744c667]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]

Debug info from gdb:


=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Aborted (core dumped)

以下代码

class MainClass
{
    public static void Main (string[] args)
    {

        StreamReader input = new StreamReader ("animals.txt");

        while (!input.EndOfStream) {    
            string line = input.ReadLine ();
            string[] data = line.Split (',');
            if (data [3] == "true") {
                Animal myAnimal = new Animal (data[0], Convert.ToInt32(data[1]), data[2], Convert.ToBoolean(data[3]));
                Console.WriteLine (myAnimal);
                }
        }
    }
}
}

public class Animal
{
    string species;
    int legs;
    string genus;
    bool extinct;

    public Animal (string species, int legs, string genus, bool extinct)
    {
        this.species = species;
        this.legs = legs;
        this.genus = genus;
        this.extinct = extinct;
    }

    public string Species{ get; set;}
    public int Legs{ get; set;}
    public string Genus{ get; set;}
    public string isExtinct{ 
        get
        {
            return isExtinct;
        }
        set 
        {
            if (extinct == true) {
                isExtinct = "extinct";
            } else {
                isExtinct = "";
            }
        }
    }

    public override string ToString(){
        return String.Format("{0} ({1} legged) {2} ({3})", species, legs, genus, isExtinct);
    }
}
}

2 个答案:

答案 0 :(得分:2)

快速修复

timestamp属性看起来像个问题......试试这个:

isExtinct

应该修复它,但我想你可能想考虑清理......

清理

例如,我认为根本没有吸气剂。只需在public string isExtinct { get { return extinct ? "extinct" : "" } } 方法中执行逻辑(因为这可能是唯一需要使用它的地方),然后您也可以使括号成为条件。类似的东西:

ToString

建议代码

此外,您的属性public override string ToString() { string result = String.Format("{0} ({1} legged) {2}", species, legs, genus); if(extinct) result += " (extinct)"; return result; } SpeciesLegs没有任何用处,无论如何它们都与您的字段无关。我建议删除字段并坚持使用4个属性:

Genus

答案 1 :(得分:2)

您的属性逻辑错误 - 您不希望两个单独的可写属性和语法不正确。

试试这个:

public string isExtinct { 
        get
        {
            if (extinct == true) {
                return "extinct";
            } else {
                return "";
            }        
        }
}