为什么此代码不显示我在方法中返回的变量?

问题描述 投票:0回答:1

代码中没有错误,但是当我尝试调用编写的方法来检查我输入的字符串时,它为空

enter image description here

public static void main(String[] args) {

    Main m = new Main();

    m.balancedStringSplit("LLRRLLLRRRRLR");

}

public int balancedStringSplit(String s)
{
    int count = 0;
    int res = 0;

    for(int i = 0; i<s.length(); i++){
        if(s.charAt(i) == 'L')
        {
            count++;
        }
        else if(s.charAt(i) != 'L')
        {
            count--;
        }

        if(count == 0)
        {
            res++;
        }
    }
    return res;


}
java
1个回答
3
投票

如上文其他指出的那样,您需要使用方法的返回值来执行某些操作。要么需要将其分配给变量,然后您就可以对其进行处理:

int i = m.balancedStringSplit("LLRRLLLRRRRLR");
System.out.println(i);
© www.soinside.com 2019 - 2024. All rights reserved.