Results 1 to 5 of 5

Thread: For Loops

  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default For Loops

    Hi guys. Hope you are well. I'm particularly stuck on understanding for loops properly. I have this program which computes numbers to whatever power you want it to. There are two command line arguments, the first being the number and the second being the power:

    Java Code:
    public class Power
    {
      public static void main(String [] args)
      {
        int number = Integer.parseInt(args[0]);
        int power = Integer.parseInt(args[1]);
        int answer = 0;
        
        for (int i = 0; i*i < power; i++)
          answer=number*power;
        System.out.println(answer);
        
      }
    }
    Can anyone help on correcting this problem and possibly explaining how the for loop works, because i'm so confused :S

    Kind regards

    Shyam

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: For Loops

    help on correcting this problem
    Can you explain what the problem is?
    Post any output and explain what is wrong with it and show what it should be.

    Have you worked out the problem on a piece of paper doing the math one operation on each line to see what the pattern is that you want in the loop?
    Last edited by Norm; 09-27-2011 at 06:19 PM.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: For Loops

    Guess you didn't like the answer you already got?

    Crossposted: For Loops
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default Re: For Loops

    I have explained it. All I want it to do is compute the answer. So If provide the arguments for e.g. 2 3 then it should be 2 to the power of 3 and should give an answer of 8. I just dont understand the for loop properly. Can someone explain what happens. The program is wrong it doesn't work.

    Kind regards

    Shyam

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: For Loops

    Have you worked out the problem on a piece of paper doing the math one operation on each line to see what the pattern is that you want in the loop?
    What are the first two numbers to multiply
    What are the next two
    etc until done.

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  2. Help with Loops
    By Spyderpig in forum New To Java
    Replies: 10
    Last Post: 02-17-2011, 09:10 AM
  3. loops
    By curioustoknow in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 02:45 PM
  4. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 07:51 PM
  5. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •