java - Missing return statement due to If condition -
so have following function:
int digitsum(int n){ int s = n; if(n < 10) return s; while(s > 0){ n = s + n % 10; s = n / 10; } digitsum(n); } i want take number , sum of digits , want keep doing until end single digit number. can understand here, if statement causing error during compilation, says missing return statement , highlights last } .
can me this?
in non-void function , every function call must trace return statement , java says
every execution path in function must lead return statement
so add return digitsum(n);
according rule in java , if condition if(n < 10) false there no further return statement exists either there should default return statement or there should other return statement in conditional else case.
Comments
Post a Comment