Java 不兼容的类型:void 不能转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28729707/
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-11 06:42:00  来源:igfitidea点击:

incompatible types: void cannot be converted to int

javaclassvoid

提问by D_Wagner

I'm extremely new when to comes to Java and programming in general. I am trying to create a simple program where you guess my age and if you are right it will say "correct" and if you are wrong it will say "wrong".

我对 Java 和一般编程非常陌生。我正在尝试创建一个简单的程序,您可以在其中猜测我的年龄,如果您猜对了,它会说“正确”,如果您错了,它会说“错误”。

This is my code:

这是我的代码:

import java.util.InputMismatchException;
import java.util.Scanner; // This will import just the Scanner class.

public class GuessAge {
    public static int main(int[] args) {
       System.out.println("\nWhat is David's Age?");
       Scanner userInputScanner = new Scanner(System.in);
       int age = userInputScanner.nextLine();



        int validInput = 20;
        if (validInput == 20) {
            return System.out.println("Correct!!");
        }
        else {
            return System.out.println("Wrong....");
        }
    }
}

I get the error "incompatible types: void cannot be converted to int" but I have no void class in the code? I know my code is probably awful but if you guys could point me in the right direction that would be great. Thanks.

我收到错误“类型不兼容:void 无法转换为 int”,但代码中没有 void 类?我知道我的代码可能很糟糕,但如果你们能指出我正确的方向,那就太好了。谢谢。

采纳答案by GermaineJason

Your program does not have to return an intin public static int main. Instead you can have it as void(meaning don't return anything). You should simply just print your statements and don't returnthem. Also the int[]should be String[]and Scannershould check for nextInt()as pointed out in comments!

您的程序不必返回intin public static int main。相反,您可以将其作为void(意思是不返回任何内容)。您应该简单地打印您的报表,而不是打印return它们。也int[]应该String[]并且Scanner应该检查nextInt()评论中指出的!

import java.util.InputMismatchException;
import java.util.Scanner; // This will import just the Scanner class.

public class GuessAge {
public static void main(String[] args) {
   System.out.println("\nWhat is David's Age?");
   Scanner userInputScanner = new Scanner(System.in);
   int age = userInputScanner.nextInt();



    int validInput = 20;

    // typo in your code - compare to age
    if (validInput == age) {
        System.out.println("Correct!!");
    }
    else {
        System.out.println("Wrong....");
    }
}

}

回答by RogueBaneling

You are trying to return System.out.println()which is of type void. Remove the returnstatements from before System.out.println(), and they will still print. Note that you do not need to specify a return value in the mainmethod.

您正在尝试返回System.out.println()类型为void。删除returnbefore 中的语句System.out.println(),它们仍然会打印。请注意,您不需要在main方法中指定返回值。

回答by user3029486

In your method declaration, you have public static int main(int[] args)

在你的方法声明中,你有 public static int main(int[] args)

The word after the statickeyword is a return type, and in this case your declaration is requiring the main method to return an int. To solve this, main should have a void return type, as you're only printing within main and not returning type int.

static关键字后面的单词是返回类型,在这种情况下,您的声明要求 main 方法返回一个int. 为了解决这个问题,main 应该有一个 void 返回类型,因为你只在 main 中打印而不是返回类型 int。