如何检查除法的结果是int还是float

时间:2017-01-08 08:43:50

标签: android math int operators

我正在制作2个生成的整数然后我将它们分开。我想检查2个除以整数的结果是整数还是小数。然后我将尝试比较结果并将结果与​​带有此代码的字符串交换,但它停止工作。

Random r = new Random();
int first = r.nextInt(50 - 1) + 1;

Random z = new Random();
int twond = z.nextInt(50 - 1) + 1;

float Resultas = (first / twond);
Result = (int) Resultas;
String floatResult = Float.toString(Resultas);

while (floatResult != String.valueOf(Result)) {
  Random s = new Random();
  first = s.nextInt(50 - 1) + 1;

  Random y = new Random();
  twond = y.nextInt(50 - 1) + 1;

  Resultas = (first / twond);
  Result = (int) Resultas;
  floatResult = Float.toString(Resultas);
}

1 个答案:

答案 0 :(得分:3)

if (first % twond == 0) {
    // result is an integer
}
相关问题