Your problem lies within the following lines
int product= length * width;
float totalsquareyards=product / 9;
Dividing an int by another int always returns an int, so you're dividing
product by 9, throwing away the fractional part, then casting that
result to a float.
If you want a floating point answer, your calculation must include a
floating point value. In this case, simply using 9.0f (that 'f' is
important) instead of 9 would be enough.
On 5/9/2010 9:25 PM, Luke Vandervort wrote:
This should be simple for you guys. I am writing a simple program. The
correct answer is 630, however java comes up with 626.4. I assume this
is a rounding issue because I used the wrong variable type, but after
a couple hours on this thing I have no idea. Anyone have any
suggestions?
public class yards
{
public static void main(String[] args)
{
byte length=25;
byte width=42;
int product= length * width;
float totalsquareyards=product / 9;
double cost= totalsquareyards * 5.4f;
System.out.println("");
System.out.print("The carpeting is " + length);
System.out.println(" feet long.");
System.out.println("");
System.out.print("The carpeting is " + width);
System.out.println(" feet wide.");
System.out.println("");
System.out.print("It will cost $" + cost);
System.out.println(" to carpet the room");
System.out.println("");
}
}
Thanks!
--
You received this message because you are subscribed to the Google Groups "The Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.