Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的方法不返回任何内容?_Java_Algorithm - Fatal编程技术网

Java 为什么我的方法不返回任何内容?

Java 为什么我的方法不返回任何内容?,java,algorithm,Java,Algorithm,为什么我的方法不返回任何内容 class Test{ static int count = 0; public static void main(String args[]){ String s = "------+ # ----+------"; countDoors(s); } public static int countDoors(String s){ char sigN= '+';

为什么我的方法不返回任何内容

class Test{
    static int count = 0;


    public static void main(String args[]){

    String s = "------+    # ----+------";
        countDoors(s);

    }

    public static int countDoors(String s){
        char sigN= '+';


        for(int i=0;i<s.length();i++)

            if(s.charAt(i)==sigN)
                count++;


        return count;

    }


}
类测试{
静态整数计数=0;
公共静态void main(字符串参数[]){
字符串s=“----+\----”;
数扇门;;
}
公共静态int countDoors(字符串s){
字符符号='+';
对于main()方法中的(inti=0;i,您调用
countDoors(s);
,它返回
count
值,但您不使用它

如果只想将此值打印到控制台,则将
countDoors(s);
更改为
System.out.println(countDoors));

如果您想将调用
countDoors
的结果保存到变量以供以后使用,下面是一个示例:

int savedValue = countDoors(s);

你怎么知道它不工作了?它正在返回一些东西,你只是没有打印结果或对它做任何事情。你在哪里调用函数?代码很好..你只需要使用System.out.println()要打印输出,请添加system.out,它肯定会打印您的计数非常感谢。我的印象是,当您返回一些东西时,它会打印出来。哈哈,谢谢兄弟!@Ooguro很高兴我能帮助您:-)