Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Why isnt this working?

    int foo = 10.9 + 2112 / 100 % 2 - 2;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please define "not working?"

    One issue, which I'm not sure is a problem: You're doing int division. 1/2 will result in 0. 1.0/2 will result in 0.5.

    Try changing 2112 to 2112.0. Also, you should use parenthesis -- when do you want the mod operator to act?

  3. #3
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Try changing 'int' to 'double'.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    wangwei is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Hi, this refers to type casting. You can cast type int to double, but reverse not, because it will lose precision. In some IDE, it will give 'Type mismatch: cannot convert from double to int' tip. If you must cast double to int, you can use the following form:
    Java Code:
    int i = (int) 22.99;
    Now, it will be 22, but not 23 and any others.
    So, you can change your formula to:
    Java Code:
    int foo = (int) 10.9 + 2112 / 100 % 2 - 2;
    and the result is 9.
    For details, you can refer some java beginners books.

Similar Threads

  1. Replies: 11
    Last Post: 12-25-2009, 11:30 PM
  2. My rotate 2d pos method isnt working correctly..
    By Addez in forum New To Java
    Replies: 5
    Last Post: 12-01-2009, 10:04 AM
  3. Replies: 6
    Last Post: 11-12-2009, 11:50 AM
  4. Replies: 8
    Last Post: 10-29-2009, 11:54 AM
  5. why isnt it sending value
    By snitdesne in forum New To Java
    Replies: 5
    Last Post: 10-24-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •