Java - 错误消息 : 'Cannot Convert from void to String

标签 java string void

我正在为作业创建一个简单的控制台小程序,但在尝试运行我的应用程序时遇到了错误。错误是:
“类型不匹配:无法从 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'");
                    }
            }   
        }
    }

最佳答案

查看这行代码

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

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

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

看起来您将 readLineprintln 放错了位置

关于Java - 错误消息 : 'Cannot Convert from void to String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11604298/

相关文章:

Java多态方法

c - 在C程序中自铸一个void指针指向int是否可能?

JavaFX GrowableDataBuffer、Canvas 性能问题

java - Autowiring map 未按预期工作

java - 使用 Aspectj 时可以在 args 中使用我自己的对象吗?

c - 如何在 C 中转换一个 void 函数指针?

java - 混合不同版本的 Java 库

javascript - 如何将 JavaScript 中的字符串转换为字节序列?

java - 测试字符串中的数字字符

c - 使用 Function 反转字符串并将整个字符串返回到主函数