为什么我的方法没有返回任何东西?
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我肯定有点菜鸟的问题,但我真的想明白为什么它不起作用
在 main() 方法中,你调用countDoors(s);,它返回count值,但你什么都不做。
如果您只想将此值打印到控制台,请更改countDoors(s);为System.out.println(countDoors(s));
如果您想保存调用countDoors(s)变量的结果以供以后使用,有一个示例如何实现它:
int savedValue = countDoors(s);
Run Code Online (Sandbox Code Playgroud)