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

Java-错误消息:';无法从void转换为String,java,string,void,Java,String,Void,我正在为作业创建一个简单的控制台小程序,在尝试运行我的应用程序时遇到错误。错误是: “类型不匹配:无法从void转换为String” 当我有一个重复出现的“ElseIf”语句时,该错误会出现四次。以下是问题所在的元素: String thirdNum = println("Would you like a third number?(Y/N)"); 以下是完整的代码: import acm.program.*; public class abacusConsole extends C

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

当我有一个重复出现的“ElseIf”语句时,该错误会出现四次。
以下是问题所在的元素:

String thirdNum = println("Would you like a third number?(Y/N)");
以下是完整的代码:

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

看起来您的println函数被声明为void(请将其签名btw发布),但您正试图将其结果(void)分配给字符串变量。

看起来您的println函数被声明为void(请将其签名btw发布),但您正试图将其结果(void)分配给字符串变量。

println()
方法没有返回类型,因此它返回
void
,您显然无法将其分配给
字符串
引用。

println()
方法没有返回类型,因此它返回
void
,您显然无法将其分配给
字符串
引用。

查看代码的这一行

String operator = readLine("Which operator would you like to use?");`
并将其与导致错误的一个进行比较

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

看这一行代码时,您似乎把
readLine
放错位置了
println

String operator = readLine("Which operator would you like to use?");`
 String thirdNum = println("Would you like a third number?(Y/N)");
并将其与导致错误的一个进行比较

String thirdNum = println("Would you like a third number?(Y/N)");
看起来您将
readLine
println

 String thirdNum = println("Would you like a third number?(Y/N)");
println或(printline)就是这样做的。它打印一行,不返回任何内容。你试着把这个空放进一个字符串。这是行不通的

你可能想用另一个

   readline
println或(printline)就是这样做的。它打印一行,不返回任何内容。你试着把这个空放进一个字符串。这是行不通的

你可能想用另一个

   readline

我想你的意思是“分配”,而不是“分配”。+1给代码学徒,因为没有一半分配。我想你的意思是“分配”,而不是“分配”。+1给代码学徒,因为没有一半分配。