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 14 of 14

Thread: In desperate need of help. Making an exponents table.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default In desperate need of help. Making an exponents table.

    Hello everyone!

    So I'm just at a wall right now. I'm a very very beginner java programmer. I currently have to write a program that displays a table. The user will enter the base number, then the maximum exponent. It will then be displayed like so:

    Enter the base: 2
    Enter the maximum exponent: 7
    The base is 2 and the maximum exponent is 7.

    Powers of 2

    x 2^x
    0 1
    1 2
    2 4
    3 8
    4 16
    5 32
    6 64
    7 128

    I have to use for loops.

    If someone could explain a loop that can carry out this function, then it would be greatly greatly appreciated.

    I can post what I currently have in order to see I am making an effort (Although the for loops are way way off.) I am not on here to ask for straight answers, but to instead build onto my knowledge of Java. Any help is appreciated and thank you very much in advance!

    Here's my code: (Totally lost)

     import java.util.*;
     
    public class Practice {
     
      public static void main(String[] args) {
     
        Scanner kb = new Scanner(System.in);
        int b = kb.nextInt();
        int y = kb.nextInt();
        int result = b ^ y;
     
     
    for(result = 1; result <= y; result = result * b){
             System.out.print("\t " + result);
             System.out.print("\n");
     
     
          }
       }
    }
    Last edited by jp93129; February 14th, 2012 at 04:35 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Please post what you have so far.
    Start with a variable with a value of 1 and multiply it by the base and save that value and then loop and do it again until done.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    There only needs to be 1 for loop. Once you collected base 'b' and exponent 'e', the for loop would run from x = 1 to x<= e. Inside the for loop just print out b and b^x.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Thanks for the replies, working on it now! Just posted the code I have now, if anyone could elaborate a little more it'd be nice. I think i know what to put in the loop now, but i don't think i understand the concept of a for loop.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Please edit your post and wrap the code in code tags
    BB Code List - Java Programming Forums - The Java Community

    You should use descriptive variable names, not x and y. The two values you are readin g in are base and exponent.

    To see how a loop works, change your code to loop exponent times and print out the value of the index each time the loop iterates.
    Last edited by Norm; February 14th, 2012 at 04:37 PM.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    I understand you up until you say "Inside the for loop just print out b and b^x. " Do you mean put that in the update part? I understand for(x=1; x<= exponent; ???)

    I also dont understand what should be in the System.out.println(???); after the loop.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: In desperate need of help. Making an exponents table.

    For testing just print out the value of the index.

    When you can get the loop working like you want, then add code to compute the value to be printed.

    what should be in the System.out.println(???)
    The variable that contains the computed amount.

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    I'm so frustrated! lol. Well I'm really having trouble with the update part of the loop now. I can't get it right.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Post the code and ask your questions about what you want it to do.
    Do things one at a time:
    make a loop that goes for exponent times
    when that works
    compute the value inside of the loop using the base and loop index
    print the value

  10. The Following User Says Thank You to Norm For This Useful Post:

    jp93129 (February 14th, 2012)

  11. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Oh i see. So there is some expression(s) I'm missing before the loop I think. I'm going to experiment some more, if I don't figure it out ill be more specific and takes things one at a time. I really appreciate your help!

  12. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    First lets change your variables to something more descriptive

        Scanner kb = new Scanner(System.in);
        int base = kb.nextInt();
        int exponent = kb.nextInt();

    Alright so before leaving the for loop in terms of x was a bad idea because it wasn't informative enough.

    Here's the basic for loop

    for (int exp = 1; exp <= exponent; x++)

    As you can see exp will start at 1, increments on each iteration and ends at the 'exponent' which was inputted by the user. This will be the first column (x) as described in your first post. Now that you have your first column (x), you just need your second column (base^x).

    So inside the loop will print 'base' (the first column) and then print 'base^exp' (your second column)


    Do you understand it now?
    Last edited by shiftasterisk; February 15th, 2012 at 10:48 AM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: In desperate need of help. Making an exponents table.

    @shiftasterisk
    Please run your code through a compiler before posting it here. Your for statement is going to confuse the OP.

  14. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Quote Originally Posted by shiftasterisk View Post
    First lets change your variables to something more descriptive

        Scanner kb = new Scanner(System.in);
        int base = kb.nextInt();
        int exponent = kb.nextInt();

    Alright so before leaving the for loop in terms of x was a bad idea because it wasn't informative enough.

    Here's the basic for loop

    for (int exp = 1; int x <= exponent; x++)

    As you can see exp will start at 1, increments on each iteration and ends at the 'exponent' which was inputted by the user. This will be the first column (x) as described in your first post. Now that you have your first column (x), you just need your second column (base^x).

    So inside the loop will print 'base' (the first column) and then print 'base^exp' (your second column)


    Do you understand it now?

    Figured it out. Notice the important piece of code that was left out after the for loop.

    import java.util.*;
     
    public class Project1 {
     
      public static void main(String[] args) {
     
        Scanner kb = new Scanner(System.in);
        int b = kb.nextInt();
        int y = kb.nextInt();
        int result = 1;
     
        for(int i = 0; i<=y; i++) {
     
    System.out.println(i + "\t" +result);
     
     
    result *= b; // pre-calculate for next test
                 }
     
     
          }
       }

  15. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: In desperate need of help. Making an exponents table.

    Quote Originally Posted by Norm View Post
    @shiftasterisk
    Please run your code through a compiler before posting it here. Your for statement is going to confuse the OP.
    yea whoops, really botched that one as i was in a rush, sorry about that. Glad the OP figured it out, I edited my post just in case for future references tho.

Similar Threads

  1. Stuck with KMeans Clustering Algorithm DESPERATE HELP NEEDED
    By Suzanne42 in forum Algorithms & Recursion
    Replies: 6
    Last Post: December 23rd, 2011, 08:29 PM
  2. Convert Int to Doubel value without exponents and decimal value.
    By project25_java in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2011, 09:36 PM
  3. Desperate Need of help
    By mszyndlar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2011, 03:32 AM
  4. programming hw desperate help!
    By midnightdream13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2011, 07:58 AM
  5. What is wrong with this code, please desperate
    By macnasty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:26 AM