| Author |
exponent loop w/out math .pow.
|
lillie field
Greenhorn
Joined: Apr 05, 2008
Posts: 2
|
|
im trying to make a calculator without using math.pow. im writing my class and i seem to be stuck. here's my code. public static int getRaiseToThePowerOf(int rpo, int n1, int n2) { rpo = 1; if (n2 < 0) { System.out.println("Error: Exponent Must Be >=0"); } else if (n2 == 0) { rpo = 1; } else if (n2 == 1) { rpo = n1; } else { return n1 * rpo(n1, n2-1); } when i try to compile i get an error saying that the compiler can't find symbol for "rpo" in the line "return n1 * rpo(n1, n2-1);". should i rename all "rpo" to "getRaiseToThePowerOf"? i'm stuck. please help.
|
 |
Muhammad Saifuddin
Ranch Hand
Joined: Dec 06, 2005
Posts: 1318
|
|
because compiler consider "rpo" as method. which is not exist actually.
should i rename all "rpo" to "getRaiseToThePowerOf"? i'm stuck. please help.
Yes, but you need to provide three parameter to getRaiseToThePowerOf method, see your method signature.
|
Saifuddin..
[Linkedin] How To Ask Questions On JavaRanch My OpenSource
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by lillie field: ...
Welcome to JavaRanch! Note that regardless of what you pass to the method as "rpo," that value is lost, because "rpo" is immediately reassigned to 1. [ April 05, 2008: Message edited by: marc weber ]
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Correct. Another problem is that you have paths that will return nothing. Observe: Also, just printing something is not a good idea. Here's a small improvement: [ April 06, 2008: Message edited by: Rob Prime ]
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
lillie field
Greenhorn
Joined: Apr 05, 2008
Posts: 2
|
|
|
thanks so much for your help!
|
 |
 |
|
|
subject: exponent loop w/out math .pow.
|
|
|