Boolean value not resetting
I am working on a recursive call that checks to see if numbers are divisible by 10 and return false if not. I can't figure out why if(g != 0) ans = false; doesn't change the value of ans in the method and pass back false to the main method. Thank you for your help.
Code:
public class MultiplesOfTen {
public static boolean dividedBy10 (int x[], int r, int y, boolean ans){
int g = (x[r]%10);
if(g != 0)
ans = false;
else if ( r < y){
r++;
dividedBy10(x, r, y, ans);
}
return ans;
}
public static void main(String[] args) {
int [] nums = {30,55,80,70,100};
int r = 0; //set for the index value
int len = nums.length -1;
boolean ans = true;
System.out.print("Are all the numbers dividable by 10?");
System.out.println("The answer is: " + dividedBy10(nums, r, len, ans));
}
}