Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default finding the power to a number then the user inputs a max a min power

    hi guys ineed some help on a basic program. the user has to input a base and a min and max of powers. from there the program has to determine all the answers including the min and max and has to output them. example: base: 2 min: 1 max: 3 so the answer should be
    2 4 8. i have started the code but i dont know where to go from here:
    Java Code:
    package numberssquared;
    import TerminalIO.KeyboardReader;
    public class Square {
        public static void main(String[] args) {
               KeyboardReader reader = new KeyboardReader();
               int base;
               int minnumber;
               int maxnumber;
               double squarenumber = 0;
               int number;
          
               base = reader.readInt("what is the base: ");
               minnumber = reader.readInt("what is the first exponent ");
               maxnumber =  reader.readInt("what is the second exponent: ");
         
               for(number = minnumber; number <= maxnumber; number ++)
         	       squarenumber = Math.pow(base, number);
    
        	   System.out.print(squarenumber); 
        	  
        } // end main()
    }  // end class
    this will just display the last power so for that exmaple it just answers 8. the for stament does work and when i tested it, it said 1 2 3 which is right. but it doesnt work when its enterd into the math.pow.
    Last edited by Norm; 11-20-2011 at 01:46 PM. Reason: Fix indentations

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    Check the formatting of your code. It is very important that you align the ending } vertically beneath the start of the statement with the beginning {. Statements within {} should be indented 3-4 spaces.

    If you properly indent and align your code you will see what the problem is.

  3. #3
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    umm i dont understand what u are saying. is it a simple } mistake becuase i checked it and there are suppose to be two at the end. but should the loop first terminate and then after that should i write the math.pow statement?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    What do you want your code to print out? Can you copy the command prompt console from when you execute the program and post it here? Add comments to it describing what is wrong with the output and show what you want the output to be.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default move along, nothing to see here

    Glad to see everyone at the forum today!
    Last edited by pbrockway2; 11-20-2011 at 01:56 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    Thanks. I bet my wife someone would tell the OP what his problem was instead of telling him how to find his problem. So now I don't have to pay.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: finding the power to a number then the user inputs a max a min power

    Whoops, sorry. I didn't read your first post properly. Knee jerked, fingers typed.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    We'll see if the OP is awake.

  9. #9
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    ok well i have given you my code. here is a test run that i did:
    what is the base: 3
    what is the first exponent 2
    what is the second exponent: 4
    81.0
    so this is saying that 3^4 is that number. however i also want it to say what 3^2 is and what 3^3 is.
    do you understand what i mean?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: finding the power to a number then the user inputs a max a min power

    So that "System.out.print(squarenumber)" is only happening once.

    Did you do what Norm suggested in #2 and indent everything between { and }? (and nothing else) What did the code look like when you did that?

  11. #11
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    um i am a little confused. didnt i indent it when i posted my code?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    Not correctly. See my post#2.
    I have since formatted it according to my standards.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    Add a println to print the value of number immediately after the for statement and see what prints out. It will show you how many times the loop goes around.

  14. #14
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    yes i did that before i posted this: what is the base: 2
    what is the first exponent 3
    what is the second exponent: 5
    3
    4
    5
    64.0
    i understand that the for statement works but why doesnt the math.pow read only the last number entered?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    why doesnt the math.pow read only the last number entered
    Add a println that prints the values of base and number just before where you call math.pow to show how many times math.pow is being called.

  16. #16
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    ok so i did that:


    Java Code:
         for(number = minnumber; number <= maxnumber; number ++)
        	 System.out.println(base); 
        	 System.out.println(number); 
        	 squarenumber = Math.pow(base,number);
        	 System.out.print(squarenumber);
    what is the base: 2
    what is the first exponent 3
    what is the second exponent: 4
    2
    2
    5
    32.0
    i dont understand what is happening. i didnt do anything to the base in the for statement.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    Why do you have lines 3, 4 and 5 indented? Do you think indenting them will put them in the for loop?

  18. #18
    vicu1 is offline Member
    Join Date
    Nov 2011
    Posts
    19
    Rep Power
    0

    Default Re: finding the power to a number then the user inputs a max a min power

    ok so do you mean i have to write another for statement? i feel like that is what i have to do. my teacher i think said there shpuld be a lot of for statements. But how would i start it? would it be something like for(squarenumber = Math.pow(base,number).......
    Last edited by vicu1; 11-20-2011 at 08:44 PM.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: finding the power to a number then the user inputs a max a min power

    No, one for statement should do it.

    Why do you have the statements on lines 3, 4 and 5 indented? Do you think indenting them will put them in the for loop?
    If those statements are outside of the for loop then they should start in the same column as the for.
    Here is the correct formatting for your code. There is one statement in the for loop. The other 3 are outside.
    Java Code:
         for(number = minnumber; number <= maxnumber; number ++)
        	 System.out.println(base); 
         System.out.println(number); 
         squarenumber = Math.pow(base,number);
         System.out.print(squarenumber);

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default Re: finding the power to a number then the user inputs a max a min power

    Java is not Python; in Python both <statement1> and <statement2> are executed each time the body of the for loop is executed:

    Java Code:
    for(number = minnumber; number <= maxnumber; number ++)
       <statement1>
       <statement2>
    Not so in Java; white space and indentation means nothing to Java. In Java the above could've been writen as:

    Java Code:
    for(number = minnumber; number <= maxnumber; number ++)
       <statement1>
    <statement2>
    Only <statement1> makes up the body of the for loop; if you want both statements to be part of the for loop, you have to use curly brackets:

    Java Code:
    for(number = minnumber; number <= maxnumber; number ++) {
       <statement1>
       <statement2>
    }
    Again, space mean nothing to Java, you could've written this instead (as long as the curly brackets are there):

    Java Code:
    for(number = 
    minnumber; number <= 
    maxnumber; number ++) { <statement1>
    <statement2> }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Power in java
    By someone in forum New To Java
    Replies: 14
    Last Post: 04-20-2011, 08:29 AM
  2. What does rep power mean in our site?
    By makpandian in forum New To Java
    Replies: 12
    Last Post: 06-27-2009, 05:42 PM
  3. Power*MatchMaker 0.9.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:21 PM
  4. Power*MatchMaker 0.9.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-17-2007, 02:05 PM
  5. Power*Architect 0.9.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-31-2007, 06:52 PM

Posting Permissions

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