Results 1 to 2 of 2
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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.

    Java 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));
    
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Two remarks:

    1) a parameter doesn't exist outside the method call;
    2) you're ignoring the method return value in the else-clause.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 11:22 AM
  2. Boolean methods
    By sonny in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 10:16 PM
  3. Boolean problems
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 09-15-2009, 10:57 PM
  4. transfer boolean to 1's and 0's
    By Nikohw in forum Java Applets
    Replies: 5
    Last Post: 09-12-2009, 09:05 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •