• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with computations

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! Thanks!

That makes loads of sense!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic