Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Last edited by integral; October 3rd, 2014 at 07:50 AM.
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
Please post your code correctly using code or highlight tags per the above link.
Do you happen to know how to solve this, Greg?
I am befuddled.
The for loop is fine. What is inside wont get you further. Youll have to figure out how to get the power of your base, which is hard. If you keep trying youll get it sometime.
Comments in your code would be extremely helpful. There's no reason someone should have to stare at your code and the desired output to figure out that the purpose of the method computPower() is to calculate the base to the power of power. Say that in a comment somewhere.
If I give you a number, let's say 3, and tell you to raise that to the power of 4, what do you do? If you only had pencil and paper, you might sit down with and do something like:
3 * 3 = 9 (3 squared)
9 * 3 = 27 (3 cubed)
27 * 3 = 81 (3 ^ 4)
or
3 * 3 * 3 * 3 = 81
How could you do that same repetitive calculation in a loop?
And don't declare the resulting variable INSIDE the loop as in this statement:
int answer=1*base;
That makes the variable 'answer' local to the loop, completely worthless outside the loop where you'll eventually need it.
Come back with your updated code.
Thanks for pointing out that i declared the int answer and print line within the for loop. I have updated my code to the best of my understanding.
I understand how to print out a regular square situation, but i do not know how to do it within parameters. What should the for loop consist of fully?
What should be the for loop to get my desired outcome?
Where? I want to see the best of your understanding. I also don't understand why you can't write a for() loop that multiplies a base by itself power times. Please try it, give it your best effort.I have updated my code to the best of my understanding.
I know it might be very simple, but i am very, very new to this Java programming thing. I literally started yesterday. I work best with examples, so i bet after i get to know what goes into that for loop that i would be more than capable applying in different fashions else where.
i hope you understand.
I think you'll find that you'll learn programming more thoroughly by trying things, discovering what works, what doesn't, and then figuring out why. Waiting for someone to show you how to accomplish a skill is either lazy or just plain wrong-headed thinking. Programming is a skill, and a skill cannot be acquired by observing someone else perform it, no matter how masterful they are. Plus, the number of Java for() loop examples on the 'net must be in the multiple 100s of thousands, so there's no reason you shouldn't have a model to follow.
Though I'm opposed (internally conflicted) here's another example of a for() loop. This one adds a number, x, to itself y times. You can use it to produce a for() loop that multiplies a number by itself some number of times.
// a loop to add x to itself y times for( int i = 1 ; i <= y ; i++ ) { result += x; }
Please come back with evidence when you've proved me wrong.I believe otherwise
Then you're not thinking . . .the for loop example that you gave me does not resonate with my problem
Good luck!