• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with loop program  RSS feed

 
Alicia Ferraro
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am currently working on a program that is supposed to calculate powers. The user enters a base and an exponent and the program will loop to calculate the powers. For example, if a user enter 3^4 the program will display 3^1, 3^2, 3^3 and 3^4. I am having a problem having my program output every power, it is only outputting the power from the base and exponent the user enters. Any advice on how I can get my program to output every power?
Screen-Shot-2015-10-14-at-2.59.53-PM.png
[Thumbnail for Screen-Shot-2015-10-14-at-2.59.53-PM.png]
The code I have written so far
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



use the above loop logic . and then according to your jTextField or whatsoever, instead of System.out.println(), use setText() method.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please don't post screenshots; yours is difficult to read and I cannot sort out the inconsistent indentation. But you appear to be working out the power once and printing the same result ten times. You would need to work it out ten times.

When you have a bit more experience you will know to avoid Math#pow for small whole numbers because x * x is quicker to execute. You can create yourself a recursive method which calculates powersI shall leave it to you to work out how to make that method resist incorrect arguments (e.g. negative exponent) and also how to make that method run in logarithmic complexity rather than linear time. The latter is quite simple but requires some lateral thinking.

[edit]Change negative to negative exponent.
 
Alicia Ferraro
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:


use the above loop logic . and then according to your jTextField or whatsoever, instead of System.out.println(), use setText() method.


I tried the code in my program and I am still just getting one output when i run the program



what else should I add to get more than one output?
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what "outputPower" is, but a method named "setText()" sounds like it should set the whole value of the thing. Whereas inside that loop you want to add text to the thing for each power, don't you? So you should look for a method which allows you to append text to whatever it is.
 
Alicia Ferraro
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm not sure what "outputPower" is, but a method named "setText()" sounds like it should set the whole value of the thing. Whereas inside that loop you want to add text to the thing for each power, don't you? So you should look for a method which allows you to append text to whatever it is.


The outputPower is the variable name for the JTextArea
 
Alicia Ferraro
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the help! I changed setText to append and now my program is working properly.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!