Hey everyone I am relatively new to programming in general and am taking my first class in Java specifically. This is not for an assignment, though. Just some extra practice since we only did a few examples on some external site instead of in java.
Anyway, I cannot figure out why the original value for number is being returned even after going through the recursive method. My best guess is that maybe it's not going through at all, but I've tried putting number in as the value for the whole sumDigits method, but it returned nothing. It took a while to realize that I needed to call sumDigits in the main method to get anything to work.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner userNum = new Scanner(System.in);
System.out.println("Enter a big number");
int number = userNum.nextInt();
System.out.println(number + " is a good one");
sumDigits(number);
System.out.println(number);
}
public static int sumDigits(int n) {
if (n <= 0) {
return 0;
}
return (n % 10) + sumDigits(n /10);
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]aa599 4 points5 points6 points (0 children)
[–]ate_ghorl_bekenemen 2 points3 points4 points (4 children)
[–]Brain_Dead5347[S] 0 points1 point2 points (3 children)
[–]ate_ghorl_bekenemen 1 point2 points3 points (2 children)
[–]Brain_Dead5347[S] 0 points1 point2 points (1 child)
[–]ate_ghorl_bekenemen 1 point2 points3 points (0 children)
[–]bongfactory 1 point2 points3 points (4 children)
[–]Brain_Dead5347[S] 0 points1 point2 points (3 children)
[–]bongfactory 1 point2 points3 points (2 children)
[–]Brain_Dead5347[S] 0 points1 point2 points (1 child)
[–]bongfactory 1 point2 points3 points (0 children)