Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
  1. #21
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by DiamondSoul View Post
    A while loop would make the program more flexible, as well as shorter. The way you have it now, it only works if the number is 3 digits or less.
    If for instance the amount of random digits never went above 3, would you still use the methods above, or would you create something more simple? I'm trying to work out these methods and i'm guessing that I could pass these methods a number with 6 digits and it would calculate the sum, whereas mine, like you say will only calculate 3 or less?

    Regards,

    NM.
    [A!B]Java

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

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by Nanomech View Post
    With the first bit of code, is line 3 calling the method from within the method?
    Yup, the method is calling itself, that's what recursive methods do; it calls itself with a smaller value of n (nearer to zero) so it always terminates because it doesn't call itself if n < 10 (one digit).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Wow some intellectual methods here, I think i'll be here in a few hours working it out haha.

    Thanks though. I think i'll read over my other threads here regarding passing in variables to methods because it's all new again.

    Regards,

    NM.
    [A!B]Java

  4. #24
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    This is what I have. Just looked over my old thread for my Math game and found loads of assistance so it is working perfect and I hope my explanation of this is somewhat near correct.

    Java Code:
    import java.util.Random;
    
    public class DigitSum {
    
         public static void main(String[] args) {
    	 
             int rNum;
    		 int result;
    	 	 Random numGen = new Random();
    	 
    	     rNum = numGen.nextInt(1000);
    	     rNum++;
    		 
    		 System.out.println("Generated Number: " + rNum);
    		 System.out.println("");
    		 System.out.println("The value of this variable gets passed into our calculate method, where the ");
    		 System.out.println("calculations are done before being returned to the main method and printing ");
    		 System.out.println("the sum of the numbers.");
    		 System.out.println("");
    	 
             result = calculate(rNum);
    		 
    		 System.out.println(result);
    		 
    	 }
    	 
    	 static int calculate(int n) {
             if (n < 10) return n; 
             return n%10+calculate(n/10);
    	 }
    }


    Many thanks,

    NM.
    [A!B]Java

  5. #25
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Java Code:
    import java.util.Random;
    
    public class DigitSum {
    
         public static void main(String[] args) {
    	 
             int rNum;
    		 int result;
    	 	 Random numGen = new Random();
    	 
    	     rNum = numGen.nextInt(1000);
    	     rNum++;
    		 
    		 System.out.println("");
    		 System.out.println("Generated Number: " + rNum);
             System.out.println("");
    	 
             result = calculate(rNum);
    		 
    		 System.out.println(result);
    		 
    	 }
    	 
    	 static int calculate(int n) {
             if (n < 10) return n; 
             return n%10+calculate(n/10);
    	 }
    }
    AHA i have worked it out!!!

    Ok so, what this all does is the value of the variable rNum is passed into the calculate method. The calculate method accepts a parameter, an int variable 'n'. I don't know how but i'm sure n holds the value of rNum. Anyway, the first line checks if the number is below 10, if so it returns n because it is only a single digit so there is no need to calculate on it. If it's larger we go to the second line.

    As an example let's say n holds 246. So we do 246%10 which is 6. we then go back into the calculate method with the result of n/10 which is 24. So now at the start of our method again, n now equals 24, so again we get to the second line 24%10 equals 4. Meanwhile these values are being added together somewhere along the way. It then takes the value of 24/10 which is 2 back into the method. Because n equals 2, the if statement is true so we automatically return n back to the calling method, where the result of the 3 added digits is saved in the result variable before being printed to the page?

    I'm sure i'm nearly there. Could someone explain at what point do the 3 numbers get added to one another and where does the value of the expression n%10 get stored? Because when we re-enter the method later on that line of code, we do another calculation n/10but again n is the original number.

    Regards,

    NM>
    Last edited by Nanomech; 10-28-2011 at 11:35 PM.
    [A!B]Java

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

    Default Re: Adding individual numbers together from a random number

    Quote Originally Posted by Nanomech View Post
    I'm sure i'm nearly there. Could someone explain at what point do the 3 numbers get added to one another and where does the value of the expression n%10 get stored? Because when we re-enter the method later on that line of code, we do another calculation n/10but again n is the original number.
    That's the mechanism of the Java stack; when you call the method calculate with a parameter 246, that number is put on the data stack and that method calls it 'n'. If n < 10 the method simply returns that value; otherwise it stores the value n%10 in a register, pushes the value 246/10 ( == 24) on the stack and calls itself; it's new 'incarnation' calls that value 'n' again. It is larger than 10 so it stores the value n%10 ( == 24%10 == 4) in a register and pushes the value n/10 ( == 24/10 == 2) on the stack again and calls itself. At this point the values 246, 24 and 2 are pushed on the stack and the latest 'incarnation' of the method decides to return that value because it's less than 10. The previous incarnation receives that return value (2) and adds it to the value (4) it kept in a register. It returns 6 which is received by the first incarnation of the method which add the return value 6 to the value it has kept in a register (also 6) and returns the result: 12. More complex recursion can make you dizzy ;-)

    kind regards,

    Jos
    Last edited by JosAH; 10-29-2011 at 09:43 AM.
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Adding individual numbers together from a random number

    Hehe I kinda understand what you mean. Like I say this method is quite sophisticated for someone of my standing but of course this information is invaluable for me to have someone explaining this.

    Thanks once again to everyone who guided me with this problem.

    Regards,

    NM.
    [A!B]Java

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Extracting individual numbers from a long integer
    By CurbYourEnthusiasm in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 03:53 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Calculating Individual Numbers
    By TheKnight in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 12:51 AM
  4. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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