JAVA:缺少返回语句

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

JAVA: missing return statement

javajavac

提问by Howcan

my main concern with this code as of right now is a missing return statement.

到目前为止,我对这段代码的主要担忧是缺少 return 语句。

public class stringstuff{

    //using charAt    
    public static String ReverseF(String n){
        String finalstring = "";
        int len = n.length();
        for (int i = 0; (i < n.length()); i++){
            finalstring += (n.charAt(len - i - 1));
        }
        System.out.println(finalstring);
    }

    public static void main(String[]args){
        ReverseF("Hello");
    }
}

Using this code I only get the error:

使用此代码我只会收到错误:

stringstuff.java:11: missing return statement
}
^
1 error

If I switch the System.out.println to return, I don't get any errors but I also don't get an answer for ReverseF("Hello");

如果我切换 System.out.println 返回,我不会收到任何错误,但我也不会收到 ReverseF("Hello"); 的答案。

回答by Louis Wasserman

You have two options: either write

你有两个选择:要么写

public static void /* not String */ ReverseF(String n){

or write return finalStringin ReverseFand use

或者写return finalStringReverseF和使用

public static void main(String[]args){
  System.out.println(ReverseF("Hello"));
}

回答by stuXnet

You get an compilation error "missing return statement" because your return statement is missing...

您收到编译错误“缺少 return 语句”,因为缺少 return 语句...

public class Stringstuff {

    // using charAt
    public static String reverseF(String n) {
        String finalstring = "";
        int len = n.length();
        for (int i = 0; i < n.length(); i++) {
            finalstring += n.charAt(len - i - 1);
        }

        return finalstring;
    }

    public static void main(String[] args) {
        System.out.println(reverseF("Hello"));
    }
}

Is it necessary that your method is returning a value? If not, just change it from String to void, do the printing in your method and rename it to something like "printReverseF", so that your method name indicate what it's doing!

你的方法是否有必要返回一个值?如果没有,只需将其从 String 更改为 void,在您的方法中进行打印并将其重命名为“printReverseF”之类的名称,以便您的方法名称表明它在做什么!

Other feedback:

其他反馈:

  • start method names with lowercase, so you can distinct them from Classes
  • for concatenation of Strings in loops, you should use a StringBuilder/StringBuffer. Then Java doesn't have to create new String-objects per iteration of your loop - this could be a huge performance problem!
  • 以小写开头的方法名称,以便您可以将它们与类区分开来
  • 为了在循环中串联字符串,您应该使用 StringBuilder/StringBuffer。然后 Java 不必在循环的每次迭代中创建新的字符串对象 - 这可能是一个巨大的性能问题!

回答by steenbergh

Let's break down the keywords in your function header:

让我们分解函数标头中的关键字:

Public - Any function has access to this function
Static - This function does not operate on object instance variables and does not require an instance of this object to be created before it can be used
String - this function will return a String
..name.. - the name of the function
(params) - input for your function

Now note that Java thinks this function will return a string. The compiler checks to see if there is a return statement of the proper type.

现在请注意,Java 认为此函数将返回一个字符串。编译器检查是否有正确类型的返回语句。

Either use a void as the functio type and print the string inside of the function (as you' re doing now), or add the return statement and move the print to the place where the function is called from (as already suggested).

要么使用 void 作为函数类型并在函数内部打印字符串(正如您现在所做的那样),要么添加 return 语句并将打印移动到调用函数的位置(如已经建议的那样)。

HTH

HTH