无法在公共静态字符串(Java)中返回语句

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

Cannot return a statement in public static String (Java)

javastringreturn

提问by cosmo

Im very new to coding and cant seem to be able to return anything. I need to convert upper case characters to lower case and vice versa. Here's my code:

我对编码很陌生,似乎无法返回任何东西。我需要将大写字符转换为小写,反之亦然。这是我的代码:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    invString(str);
    sc.close();

}

private static String invString(String str) {
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch > 97) {
            ch = Character.toUpperCase(ch);
            return str;
        } else {
            ch = Character.toLowerCase(ch);
            return str;
        }
    }
    return null;
}

What am i doing wrong? ( in terms of returning, the code isnt complete yet)

我究竟做错了什么?(在返回方面,代码尚未完成)

EDIT****************

编辑****************

thanks for the helpful remarks, as i understood i do not have a place where my modifications are stored, so i added String res = "";and kept adding the values into String resbut in the end, when returning res, i still dont get any output...Here is the whole thing:

感谢您的有用评论,据我所知,我没有存储修改的地方,所以我添加 String res = "";并不断添加值,String res但最后,当返回 res 时,我仍然没有得到任何输出......这里是整个东西:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    String st = invString(str);
    sc.close();

}

private static String invString(String str) {
    String res = "";
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);

        if (65 <= ch && ch <= 90) {
            ch += 32;
            res += ch;

        } else if (97 <= ch && ch <= 122) {
            ch -= 32;
            res += ch;
        }


    }

    return res;
}

ps. Im not using the ready methods because my task asks for it.

附:我没有使用现成的方法,因为我的任务需要它。

回答by progyammer

There are a number of flaws in your code. Firstly, you are attempting to return the original string strin every ifstatement. So what happens is the method invString( String )simply returns the original string that is passed as argument. Instead, you should keep adding the characters to a new String variable (as @Massimosaid). Also, you are returning nullin the end. Why even do that? You would want to return the new String variable instead.

您的代码中有许多缺陷。首先,您试图str在每个if语句中返回原始字符串。因此,该方法invString( String )仅返回作为参数传递的原始字符串。相反,您应该继续将字符添加到新的 String 变量中(如@Massimo所说)。此外,你null最终会回来。为什么还要这样做?您可能想要返回新的 String 变量。

private static String invString(String str) {

    String s=""; //new String variable
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch > 97) {
            ch = Character.toUpperCase(ch);
            s+=ch; //do this instead of returning str
        } else {
            ch = Character.toLowerCase(ch);
            s+=ch; //same here
        }
    }
    return s; //return the new String
}

Secondly, in your main method, simply calling the method is wrong as it returns a value. You should assign the value returned by invString()to a String variable.

其次,在您的 main 方法中,简单地调用该方法是错误的,因为它返回一个值。您应该将返回的值分配给invString()String 变量。

public static void main(String[] args){
    ...
    String st = invString(str); //st stores the value of str with 
                                //letters' cases changed
}

回答by Massimo

You return you str object without updating it at all. You should generate a new string in which put the characters for which you reverse the case. Check the last answers in How can I invert the case of a String in Java?

您返回 str 对象而根本不更新它。您应该生成一个新字符串,在其中放置要反转大小写的字符。检查如何在 Java 中反转字符串的大小写中的最后一个答案

回答by djd0

If you want to use the inverted string, you need to actually use the returned value, e.g:

如果要使用反转字符串,则需要实际使用返回值,例如:

str = invString (str)

Strings are immutable so you can't change the characters within them. You can only replace the string variable with a new string.

字符串是不可变的,因此您无法更改其中的字符。您只能用新字符串替换字符串变量。

Modifying the characters you are accessing doesn't affect the string. You need to build a new string (look up StringBuilder) and return it.

修改您正在访问的字符不会影响字符串。您需要构建一个新字符串(查找StringBuilder)并返回它。