2010-10-28 50 views
1

它一直給我一個錯誤,在「虛空類型不允許在這裏。」錯誤「此處不允許使用虛空類型」。請幫助我

public class BugTester { 
    public static void main(String[] args) { 
     Bug bugsy = new Bug(10); 
     bugsy.move(); //now the position is 11 
     bugsy.turn();  
     bugsy.move(); //now the position is 10 
     bugsy.move(); 
     bugsy.move(); 
     bugsy.move(); 

     // Error message highlights this. 
     System.out.println("The bug position is at "+bugsy.move()); 
    } 
} 

回答

0

您還有:

class Bug { 
    public void move() { 
     // ... 
    } 
} 

如果你想printlnbugsy.move(),然後讓move()回報的位置,而不是void

2

Bug的move()方法不返回值。這是一個無效的方法。你不能連接任何東西到一個字符串。

也許有另一種Bug的方法,你想打印像getPosition()的值?

0

常見的方法是將重寫爲包含位置的Bug對象的主要信息的toString()方法。

class Bug{ 

    @Override 
    public String toString(){ 
    return "position="+position;//If you have other attributes to show, modify the mothod rather than modifying its invoker 
    } 

} 

然後調用System.out.println("The bug is "+bugsy);