Computer Science Canada counter |
Author: | starlight [ Fri Feb 03, 2006 7:39 pm ] | ||
Post subject: | counter | ||
method returns the value of the digit that is position places from the right in the decimal representation of n. e.g Enter Number: 8574 What digit do you want? 2 The digit is 5 This is what i got so far . dont' really understand why it is not working.
|
Author: | wtd [ Fri Feb 03, 2006 7:45 pm ] |
Post subject: | |
How about properly formatting your code, and using proper naming (class names should be capitalized)? |
Author: | starlight [ Fri Feb 03, 2006 8:00 pm ] | ||
Post subject: | Re: counter | ||
starlight wrote: method returns the value of the digit that is position places from the right in the decimal representation of n.
e.g Enter Number: 8574 What digit do you want? 2 The digit is 5 This is what i got so far . dont' really understand why it is not working.
|
Author: | [Gandalf] [ Fri Feb 03, 2006 9:00 pm ] | ||
Post subject: | |||
No... Something like this would do:
Now, what is In? |
Author: | wtd [ Fri Feb 03, 2006 9:12 pm ] |
Post subject: | |
[Gandalf] wrote: Now, what is In?
Something specific to the HSA classes, I imagine. starlight, step through a call of your method. digit(8574, 2) position is 2, so the conditional is met loop with i = 1 position = 4 n = 857 loop with i = 2 position = 7 n = 85 loop with i = 3 position = 5 n = 8 loop with i = 4 position = 8 n = 0 loop with i = 5 position = 0 n = 0 loop with i = 6, but 6 > 0, so end loop return 0 That doesn't seem quite right, does it? |
Author: | [Gandalf] [ Fri Feb 03, 2006 9:37 pm ] |
Post subject: | |
Quote: position = n; %10; you commented out the semicolon here
Ooops, no you didn't. % is the modulus operator in Java... Damn - too much Turing . |
Author: | starlight [ Fri Feb 03, 2006 9:56 pm ] |
Post subject: | |
i don't get it. isn't Gandalf's solution exactly the same as the one i was doing? but it keep returning only the last value. it only run the loop once but i don't understand why. |
Author: | wtd [ Fri Feb 03, 2006 9:59 pm ] |
Post subject: | |
Gandalf changed how the code works slightly. Think about the code. Think about what's going to happen when you call the method. What happens to the variables involved. You can trace out that behavior on paper. |
Author: | mercuryy [ Sat Feb 04, 2006 1:46 pm ] |
Post subject: | |
I did this program long time ago. i use 40 something lines and my teacher said there's a easy way to do it. I think u should use exponent or something. PM me if anyone knows the short way to do it. |