相关疑难解决方法(0)

如何使2个整数的除法产生一个浮点而不是另一个整数?

在计算速度的另一个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)

java

138
推荐指数
3
解决办法
15万
查看次数

预期结果在1和0之间的除法总是给出0

可能重复:
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

我不知道发生了什么事.

math integer objective-c division

-1
推荐指数
1
解决办法
2207
查看次数

标签 统计

division ×1

integer ×1

java ×1

math ×1

objective-c ×1