Why is it that this code gives me a possible loss of precision error?
public class ship {
public static void main(
String[] args){
float cost=0, weight=0;
System.out.print("Please enter weight of parcel(kg): ");
weight=Keyboard.readFloat();
if(weight<2.5)
cost=weight*3.5;
else if(weight<5)
cost=weight*2.85;
else if(weight>5)
cost=weight*2.45;
System.out.println("The cost to ship will be $"+cost);
}
}
To not get the error I must cast it as a float?
cost=(float)(weight*2.45);
I don't quite understand this when both cost and weight are both floats.
[This message has been edited by Scott Callori (edited December 03, 2001).]