2016-04-22 14 views
0

逗人,我要實現此行爲:的StackOverflowError在簡單的程序

「侵入者就會出手,倖存者將被再次出手」

但我得到這個堆棧跟蹤:

Exception in thread "main" java.lang.StackOverflowError 
    at java.lang.String.equals(String.java:975) 
    at test.Person.isDead(Person.java:14) 
    at test.Shooter.shoot(Shooter.java:7) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 
    at test.Shooter.shoot(Shooter.java:8) 

的'財產' 類:

package test; 

public class Property { 

    private Shooter shooter = new Shooter(); 

    public void punish(Person tresspasser) { 

     shooter.shoot(tresspasser); 
    } 
} 

的射擊類:

package test; 

public class Shooter { 

    public void shoot(Person person) { 

     if(!person.isDead()){ 
      shoot(person); 
     } 
    } 
} 

的 '人' 類:

package test; 

public class Person { 

    private String name; 

    public Person(String name) { 

     this.name = name; 
    } 

    public void tresspass(Property property) { 

     property.punish(this); 
    } 

    public boolean isDead(){ 

     return !name.equals("Chuck Norris"); 
    } 
} 

最後,主類:

package test; 

public class Main { 

    public static void main(String args[]) { 

     Person person = new Person("Chuck Norris"); 

     Property myProperty = new Property(); 

     person.tresspass(myProperty); 
    } 
} 

我在做什麼錯?如果這個人的名字是「查克·諾里斯」,因此你無限循環

我使用Eclipse和Java 6中,7和8出現問題...

S.

+1

s /查克諾里斯/喬恩Skeet/ – Mureinik

+0

這是在調試器中單步執行代碼將幫助你明白爲什麼它永遠遞歸。 –

回答

3

你的錯誤在你的Person構造函數中。添加條款

if(name.equals("Chuck Norris")){ 
    throw new ChuckNorrisException("Chuck Norris saw through your ploy to consider him a person"); 
} 

然後你會沒事的。

+3

我總是被告知不要拋出ChuckNorrisException,否則ChuckNorrisException會拋出我...... – Bamboomy

10

return !name.equals("Chuck Norris");始終返回false。

你可能有有限的彈藥,所以你應該考慮某種彈藥的功能。

+4

這個人是不朽的。即使你有無限的彈藥,你將會一輩子一輩子都在用他的整個生命。 –

+0

http://codesqueeze.com/the-ultimate-top-25-chuck-norris-the-programmer-jokes/ – SDsolar

3

在下面的代碼片段中,您在shoot上循環,而這個人還活着。由於查克·諾里斯不能死,你就會對他無限的拍攝,他結束了與StackOverflowError殺死你的程序:

public void shoot(Person person) { 

    if(!person.isDead()){ 
     shoot(person); 
    } 
} 

您可以設置彈藥的最大數量或添加firstShot參數,只允許2張。例如:

public void shoot(Person person) { 
    shoot(person, true); 
} 

private void shoot(Person person, boolean firstShot) { 
    if(firstShot && !person.isDead()) { 
     shoot(person, false); 
    } 
} 
+0

但他不會生氣嗎? – Bamboomy

+2

如果你只射擊他兩次,我想他可能沒有注意到它。所以你應該安全。 –