• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Help with loop program

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76397
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 27366
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the help! I changed setText to append and now my program is working properly.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic