Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By milovan

Thread: code help

  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default code help

    hi i have got this code and i got it to equal 2000 but i need it to equal 1000 can somebody please help me

    many thanks..

    /calculate and output half of my savings
    public class Savings
    {
    //your share is 50% of my savings
    public static void main (String args[])
    {
    int mySavings = 2000;
    int yourPercentage = 50;

    int yourShare = (mySavings + (yourPercentage / 100));

    System.out.println("Your share: " + yourShare);
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: code help

    Multiplication is used to find a percentage of some number, not addition.

  3. #3
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: code help

    i put in * m8 and i got an answer of 0?

  4. #4
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: code help

    Try replacing your code:

    int yourShare = (mySavings + (yourPercentage / 100));

    with:

    int yourShare = (mySavings * (yourPercentage / 100));

    your code should produce 2000.5 This code produces 1000.

    Golfnut

  5. #5
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: code help

    //calculate and output half of my savings
    public class Savings
    {
    //your share is 50% of my savings
    public static void main (String args[])
    {
    int mySavings = 2000;
    int yourPercentage = 50;

    int yourShare = (mySavings*(yourPercentage / 100));

    System.out.println("Your share: " + yourShare);
    }
    }

    this is the code and i am still getting 0 as my answer

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    291
    Rep Power
    4

    Default Re: code help

    A hint:

    what is an Integer from 50 / 100?
    Last edited by milovan; 10-07-2011 at 12:19 AM. Reason: better example
    sunde887 likes this.

  7. #7
    Golfnutest is offline Member
    Join Date
    Oct 2011
    Location
    Washington State
    Posts
    8
    Rep Power
    0

    Default Re: code help

    Duh. Not sure if this would be the preferred way to go about this, but this works:

    double mySavings = 2000;
    double yourPercentage = 50;
    double yourShare = mySavings * (yourPercentage / 100);

    System.out.println("Your share: " + yourShare);
    }
    }

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: code help

    It has to do with how integer division works. Integer division uses truncation of the remainder. If you divide 9 by 5, you get 1.8, but integer division truncates the decimal part. You would get 1 as an answer. While Golfnut tests solution works (side note to golfnut -- don't spoon feed, and use code tags [code] YOUR CODE HERE [/code]), sometimes it's not feasible to have everything be an integer. In this case you can cast one of the numbers to a floating point form (double or float). Integer division only occurs with 2 integers, if there exists one non integer, the compiler will handle everything for you.

    Java Code:
    (double) 5 / 2;
    is fine in most cases.
    Last edited by sunde887; 10-07-2011 at 04:21 AM.

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •