在计算速度的另一个Bruce Eckels练习中,v = s / ts和t是整数.我如何制作它以便分裂曲柄浮出水面?
class CalcV {
float v;
float calcV(int s, int t) {
v = s / t;
return v;
} //end calcV
}
public class PassObject {
public static void main (String[] args ) {
int distance;
distance = 4;
int t;
t = 3;
float outV;
CalcV v = new CalcV();
outV = v.calcV(distance, t);
System.out.println("velocity : " + outV);
} //end main
}//end class
Run Code Online (Sandbox Code Playgroud) 可能重复:
C中整数除法的行为是什么?
当我使用应该在1和0之间的答案进行除法计算时,它总是返回0.
试试这个:
float a = 1;
float b = 2;
float c = a / b; //variable divide by variable gives 0.5 as it should
float d = 1 / 2; //number divide by number gives 0
float e = 4 / 2;
NSLog(@"%f %f %f %f %f", a,b,c, d, e);
Run Code Online (Sandbox Code Playgroud)
我从控制台得到这个:
2013-01-17 14:24:17.512 MyProject [1798:c07] 1.000000 2.000000 0.500000 0.000000 0.500000
我不知道发生了什么事.