此处不允许使用“'void'类型”错误(Java) [英] "'void' type not allowed here" error (Java)

查看:908
本文介绍了此处不允许使用“'void'类型”错误(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译时:

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

我真的不知道如何开始询问关于这个,除了要问:

I don't really know how to start asking about this other than to ask:


  • 这里出了什么问题?

  • 此错误消息的含义是什么?

推荐答案

如果方法返回 void ,则无需打印,因此出现此错误消息。由于printPoint已经将数据打印到控制台,您应该直接调用它:

If a method returns void then there is nothing to print, hence this error message. Since printPoint already prints data to the console, you should just call it directly:

printPoint (blank); 

这篇关于此处不允许使用“'void'类型”错误(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆