Results 21 to 27 of 27
 10282011, 09:49 PM #21
Re: Adding individual numbers together from a random number
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
 10282011, 09:51 PM #22
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
Re: Adding individual numbers together from a random number
Build a wall around Donald Trump; I'll pay for it.
 10282011, 10:05 PM #23
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
 10282011, 11:51 PM #24
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
 10292011, 12:30 AM #25
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); } }
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 reenter 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; 10292011 at 12:35 AM.
[A!B]Java
 10292011, 09:34 AM #26
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
Re: Adding individual numbers together from a random 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,
JosLast edited by JosAH; 10292011 at 10:43 AM.
Build a wall around Donald Trump; I'll pay for it.
 10302011, 07:01 AM #27
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
Similar Threads

Extracting individual numbers from a long integer
By CurbYourEnthusiasm in forum New To JavaReplies: 6Last Post: 02182011, 04:53 PM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 06:50 PM 
Calculating Individual Numbers
By TheKnight in forum New To JavaReplies: 2Last Post: 01302009, 01:51 AM 
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09152008, 03:20 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks