当我尝试编译时:
import java.awt.* ; class obj { public static void printPoint (Point p) { System.out.println ("(" + p.x + ", " + p.y + ")"); } public static void main (String[]arg) { Point blank = new Point (3,4) ; System.out.println (printPoint (blank)) ; } }
我收到此错误:
obj.java:12: 'void' type not allowed here System.out.println (printPoint (blank)) ; ^ 1 error
除了问:我真的不知道如何开始问这个:
如果方法返回void,则没有任何可打印的内容,因此此错误消息。由于printPoint已经将数据打印到控制台,因此你应该直接调用它:
void
printPoint (blank);
你正在尝试打印printPoint不返回任何结果的结果。你将需要更改代码以执行以下两项操作:
class obj { public static void printPoint (Point p) { System.out.println ("(" + p.x + ", " + p.y + ")"); } public static void main (String[]arg) { Point blank = new Point (3,4) ; printPoint (blank) ; } }
或这个:
class obj { public static String printPoint (Point p) { return "(" + p.x + ", " + p.y + ")"; } public static void main (String[]arg) { Point blank = new Point (3,4) ; System.out.println (printPoint (blank)) ; } }