【问题标题】:Java - Error Message: 'Cannot Convert from void to String [duplicate]Java - 错误消息:'无法从 void 转换为字符串 [重复]
【发布时间】:2012-07-21 04:52:08
【问题描述】:

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

【问题讨论】:

    标签: java string void


    【解决方案1】:

    看起来您的 println 函数被声明为 void(请附上它的签名 btw),但您正试图将其结果 (void) 分配给 String 变量。

    【讨论】:

    • 我认为您的意思是“分配”,而不是“分配”。
    • +1 给 Code-Apprentice 以防止半途而废。
    【解决方案2】:

    println() 方法没有返回类型,所以它返回void,显然你不能将它分配给String 引用。

    【讨论】:

      【解决方案3】:

      看看你的这行代码

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

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

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

      您似乎将readLineprintln 混淆了

      【讨论】:

        【解决方案4】:
         String thirdNum = println("Would you like a third number?(Y/N)");
        

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

        你可能打算使用另一个

           readline
        

        【讨论】:

          猜你喜欢
          • 2012-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-31
          • 2019-05-11
          • 2022-01-24
          • 1970-01-01
          相关资源
          最近更新 更多