2016-08-15 61 views
0

此代码应该输出线,说得到一个很奇怪的逻辑错误,我不明白

TREX(2条腿)恐龙(灭绝)

对于来自文件动物名单。 如果动物没有灭绝,它会被if语句忽略。

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

取而代之,它表示“真”或“假”,这取决于我如何设置isExtinct属性。

现在我已经把一个if语句到isExtinct属性输出 “灭绝”,但我得到一个错误后编译

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); 
    } 
} 
} 
+0

第一猜测是isextinct肯定你只需要一个getter。”如果空 – BugFinder

+3

你得到一个'StackOverflowException '因为'isExtinct'的getter返回'isExtinct'(相同的属性)导致无限递归。 –

回答

2

你的财产的逻辑是错误的 - 你不想两个独立的可写性和你的语法不正确。

试试这个:使用“设置”信息,如回归“灭绝”,如果真和“

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

这工作,谢谢。我不得不将set {}添加到最后。出于某种原因,C#将不允许空集;声明。上帝我想念Java! –

+0

@hggohh - C#允许没有'set'的属性。你有错误吗? –

2

快速修复

那么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,并且Genus没有任何用处,他们不依赖于你的域呢。我建议删除字段,并只用4个属性坚持:

public string Species{ get; set;} 
public int Legs{ get; set;} 
public string Genus{ get; set;} 
public string IsExtinct { get; set;} 

public Animal (string species, int legs, string genus, bool isExtinct) 
{ 
    Species = species; 
    Legs = legs; 
    Genus = genus; 
    IsExtinct = isExtinct; 
} 

public override string ToString() 
{ 
    string result = String.Format("{0} ({1} legged) {2}", Species, Legs, Genus); 
    if(IsExtinct) 
     result += " (extinct)"; 

    return result; 
} 
+0

我同意这一点,通常不会在属性中应用逻辑,但我已经被指示到这里。来自java,我只是添加了一个方法来比较2然后通过 管道输出... public override String toString(){ } –

相关问题