“此处不允许使用‘void’类型”错误(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2442675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:43:25  来源:igfitidea点击:

"'void' type not allowed here" error (Java)

java

提问by David

When I try to compile this:

当我尝试编译它时:

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

I get this error:

我收到此错误:

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:

除了问:我真的不知道如何开始询问这个问题:

  • What went wrong here?
  • What does this error message mean?
  • 这里出了什么问题?
  • 这个错误信息是什么意思?

采纳答案by Justin Ethier

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

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

printPoint (blank); 

回答by Andrew Hare

You are trying to print the result of printPointwhich doesn't return anything. You will need to change your code to do either of these two things:

您正在尝试打印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) ;
    }
}

or this:

或这个:

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

回答by Carl Manaster

You are passing the result of printPoint()- which is void - to the println()function.

您将printPoint()- 无效 -的结果传递给println()函数。

回答by Ben Zotto

The type problem is that printlntakes a String to print, but instead of a string, you're calling the printPointmethod which is returning void.

类型问题是println需要一个 String 来打印,但不是一个字符串,而是调用printPoint返回void.

You can just call printPoint(blank);in your main function and leave it at that.

你可以只调用printPoint(blank);你的主函数并保留它。

回答by ggf31416

printPointprints by itself rather than returning a string. To fix that call printPoint(blank) without the System.out.println.

printPoint打印本身而不是返回一个字符串。要在printPoint没有System.out.println.

A better alternative may be: make printPoint(Point p)return a string (and change its name to something like FormatPoint), that way the method may be used to format a point for the console, GUI, print, etc rather than being tied to the console.

更好的选择可能是:makeprintPoint(Point p)返回一个字符串(并将其名称更改为类似FormatPoint),这样该方法可用于格式化控制台、GUI、打印等的点,而不是绑定到控制台。

回答by fastcodejava

You probably wanted to do : printPoint (blank);. Looks like you are trying to print twice; once inside printPoint()and once inside main().

你可能想做:printPoint (blank);。看起来您正在尝试打印两次;一次进去printPoint(),一次进去main()