This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with computations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with computations" Watch "Help with computations" New topic
Author

Help with computations

Casey J. Buresh
Greenhorn

Joined: Mar 23, 2013
Posts: 5
I'm currently working on an assignment for my Java class, it's supposed to do this:
Write a Java program to read in an integer number (as a String) from the program arguments, and print the value of the following calculation, where n is the number:

if n < 0 - print out (n^2) + (n/2)
if n >= 0 and is even - print out (n^3)*(n-1)
if n >= 0 and is odd - print out (n^-1) + (3n)

The problem is that it doesn't seem to compute the numbers correctly.
When I input -2, it results in 3, which is correct, but when I put in -1, it results in 1, when it should be .5

That seems to be the only part that isn't working and I can't figure out why. Any help is greatly appreciated


Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5
Java supports integer division.
An operation with both operands int yields an int as a result including the case of the operation division.
That is, 1 / 2 will yield 0.



as opposed to

Casey J. Buresh
Greenhorn

Joined: Mar 23, 2013
Posts: 5
Awesome! Thanks!

That makes loads of sense!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with computations
 
Similar Threads
Print Statement help
Requesting a Code Review
factors problem
Need help with doing a calculation in Java
Compiled but I need a little more help :[