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.

Results 1 to 12 of 12

Thread: What is wrong with my code? Simple for loop?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code? Simple for loop?

    Last edited by integral; October 3rd, 2014 at 07:50 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? Simple for loop?

    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.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? Simple for loop?

    Do you happen to know how to solve this, Greg?
    I am befuddled.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    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.

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? Simple for loop?

    Quote Originally Posted by Niels van Ee View Post
    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.
    Can you help me on solving for what goes within that for loop? I mean, I tried placing other digits, but I do not know how to make a for loop that reads the power and multiply it by the base it is.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? Simple for loop?

    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.

  7. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? Simple for loop?

    Quote Originally Posted by GregBrannon View Post
    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?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? Simple for loop?

    I have updated my code to the best of my understanding.
    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.

  9. #9
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? Simple for loop?

    Quote Originally Posted by GregBrannon View Post
    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 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.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? Simple for loop?

    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;
            }

  11. #11
    Junior Member
    Join Date
    Oct 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code? Simple for loop?

    Quote Originally Posted by GregBrannon View Post
    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;
            }
    I believe otherwise, and the for loop example that you gave me does not resonate with my problem. Don't sweat it. I'll just forum else where.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code? Simple for loop?

    I believe otherwise
    Please come back with evidence when you've proved me wrong.
    the for loop example that you gave me does not resonate with my problem
    Then you're not thinking . . .

    Good luck!

Similar Threads

  1. [SOLVED] whats wrong with my simple code?
    By DaHappy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 1st, 2013, 01:41 PM
  2. I need a simple loop code
    By blacice1 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: March 7th, 2013, 05:47 PM
  3. how do i loop this simple java code?
    By legend101z in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2013, 06:46 PM
  4. I dont see anything wrong with this(simple beginner code)
    By Olympaphibian89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 26th, 2012, 03:11 PM
  5. Something wrong with really,really simple code.
    By Programmer142 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 08:01 AM