Java - 错误消息:'无法从 void 转换为 String

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

Java - Error Message: 'Cannot Convert from void to String

javastringvoid

提问by Joel Biffin

I am creating a simple console applet for an assignment and I have ran into an error when trying to run my app. The error is:
'Type mismatch: cannot convert from void to String'.

我正在为一项任务创建一个简单的控制台小程序,但在尝试运行我的应用程序时遇到了错误。错误是:
“类型不匹配:无法从 void 转换为 String”。

The error occurs four times as I have a recurring 'ElseIf' statement.
Here is the element where the problem is:

该错误出现四次,因为我有一个重复的“ElseIf”语句。
这是问题所在的元素:

String thirdNum = println("Would you like a third number?(Y/N)");

Here is the complete code:

这是完整的代码:

import acm.program.*;

    public class abacusConsole extends ConsoleProgram {
        public void run() {
            println("This program is a basic calculator two numbers!");
            println("Operations that the program recognises: \n Add \n Subtract \n Multiply \n Divide");
            String operator = readLine("Which operator would you like to use?");

            if (operator.equals("Add")) {
                int n1 = readInt("First Number");
                int n2 = readInt("Second Number");
                String thirdNum = println("Would you like a third number?(Y/N)");
                if (thirdNum.equals("Y")) {
                    int n3 = readInt("Third Number:");
                    int total = n1 + n2 + n3;
                    println(+n1+" + "+n2+" + "+n3+" = "+total+".");
                } else if (thirdNum.equals("N")) {
                    int total = n1 + n2;
                    println(+n1+" + "+n2+" = "+total+".");
                } else {
                    println("Please reload the application and type in either 'Y' or 'N'");
                }
            } else if (operator.equals("Subtract")) {
                int n1 = readInt("First Number");
                int n2 = readInt("Second Number");
                String thirdNum = println("Would you like a third number?(Y/N)");
                if (thirdNum.equals("Y")) {
                    int n3 = readInt("Third Number:");
                    int total = n1 - n2 - n3;
                    println(+n1+" - "+n2+" - "+n3+" = "+total+".");
                } else if (thirdNum.equals("N")) {
                    int total = n1 + n2;
                    println(+n1+" - "+n2+" = "+total+".");
                } else {
                    println("Please reload the application and type in either 'Y' or 'N'");
                }
            } else if (operator.equals("Multiply")) {
                int n1 = readInt("First Number");
                int n2 = readInt("Second Number");
                String thirdNum = println("Would you like a third number?(Y/N)");
                if (thirdNum.equals("Y")) {
                    int n3 = readInt("Third Number:");
                    int total = n1 * n2 * n3;
                    println(+n1+" x "+n2+" x "+n3+" = "+total+".");
                } else if (thirdNum.equals("N")) {
                    int total = n1 * n2;
                    println(+n1+" x "+n2+" = "+total+".");
                } else {
                    println("Please reload the application and type in either 'Y' or 'N'");
                }
            } else if (operator.equals("Divide")) {
                    int n1 = readInt("First Number");
                    int n2 = readInt("Second Number");
                    String thirdNum = println("Would you like a third number?(Y/N)");
                    if (thirdNum.equals("Y")) {
                        int n3 = readInt("Third Number:");
                        int total = n1 / n2 / n3;
                        println(+n1+" / "+n2+" / "+n3+" = "+total+".");
                    } else if (thirdNum.equals("N")) {
                        int total = n1 / n2;
                        println(+n1+" / "+n2+" = "+total+".");
                    } else {
                        println("Please reload the application and type in either 'Y' or 'N'");
                    }
            }   
        }
    }

回答by La bla bla

looking at this line of your code

看看你的这行代码

String operator = readLine("Which operator would you like to use?");`

and comparing it with the one which causes the error

并将其与导致错误的那个进行比较

String thirdNum = println("Would you like a third number?(Y/N)");

It looks like you misplaced readLinewith println

看起来你放错readLine了地方println

回答by Kai Mattern

 String thirdNum = println("Would you like a third number?(Y/N)");

println or (printline) does exactly that. It prints a line and returns nothing. You try to put that nothing (void) into a string. That does not work.

println 或 (printline) 正是这样做的。它打印一行并且不返回任何内容。您尝试将任何内容(空)放入字符串中。那行不通。

You probably meant to use another

你可能打算使用另一个

   readline

回答by dbf

Looks like your println function is declared as void (please post it's signature btw), but you are trying to assing it's result (void) to a String variable.

看起来您的 println 函数被声明为 void(顺便说一下,请张贴它的签名),但是您正试图将它的结果 (void) 分配给 String 变量。

回答by Juvanis

println()method has no return type, so it returns voidand you cannot assign it to a Stringreference obviously.

println()方法没有返回类型,因此它返回void并且您String显然不能将其分配给引用。