我有这个代码:
static int countStu = 0;
public static int countStudent(Node<Student> lst) {
// pre : true
// post : res = number of students in list
if (lst != null) {
countStu++;
countStudent(lst.getNext());
}
return countStu;
}
Run Code Online (Sandbox Code Playgroud)
这个方法的问题是我必须countStu
在countStudent()
方法之外声明,这在我想要调用countStudent()
两次的情况下不好,它会使返回的值加倍.如何解决此问题并能够countStudent()
无限次调用正确的结果?
归档时间: |
|
查看次数: |
433 次 |
最近记录: |