• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reverse Java Code  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again guys.
So I've got a code which performs a basic mathematical function to produce a final number. I thought it was working fine, until posting this, it seems evident there are some issues.

The next task I've got is to reverse the process. I.E I've got a list of results I need to produce by working in reverse order. I need to figure out the original number.

This should be easy, I've done algebra in school, and could do most of it on paper. Although the "While" line is really putting me off, and I don't know how to approach it.

Can someone give me some advice, or provide some insight to something I may be overlooking?

I need to produce the following integers/doubles (12.0, 15.75, 19.5, 23.25, 27.0)

Regards,

Cory








 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume the bit missed out of line 7 is intentional; you should always post the real code, otherwise you might introduce other errors which confuse us when we are answering.
In order to understand the while loop, try reading line 9 out loud.

So we can see whether your function is working, please tell us what the algorithm is. You are not getting integers or doubles there, only doubles. A number ending nnn.0 is not an integer, even if its fractional part is zero.
 
Amelia Martin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code can be used to reverse a number.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Amelia, welcome to the Ranch!

When posting code it's recommended to UseCodeTags to help make it readable. I've added them for you this time, see how much better it looks.

Now, back to the topic in hand. I don't think the program you have posted is in any way related to the original question in this thread. Perhaps you misread the question?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys.
Think I've figured out my issue.
Thanks for your help!
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cory Marcus wrote:Hey guys.
Think I've figured out my issue.
Thanks for your help!


Hi Cory,
Here is an easier way to generate those numbers:




Now, your method posted above has some errors:
- it produces just one number, because of the return statements
- if the return statements were to be removed, it would produce more numbers than the list in your initial post
I have refactored your method a bit, and removed the return statements:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!