Results 1 to 8 of 8
- 10-06-2011, 09:37 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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);
}
}
- 10-06-2011, 10:01 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: code help
Multiplication is used to find a percentage of some number, not addition.
- 10-06-2011, 10:26 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: code help
i put in * m8 and i got an answer of 0?
- 10-06-2011, 11:29 PM #4
Member
- Join Date
- Oct 2011
- Location
- Washington State
- Posts
- 8
- Rep Power
- 0
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
- 10-06-2011, 11:47 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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
- 10-07-2011, 12:18 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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
- 10-07-2011, 04:02 AM #7
Member
- Join Date
- Oct 2011
- Location
- Washington State
- Posts
- 8
- Rep Power
- 0
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);
}
}
- 10-07-2011, 04:06 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
is fine in most cases.Java Code:(double) 5 / 2;
Last edited by sunde887; 10-07-2011 at 04:21 AM.
Similar Threads
-
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks