【问题标题】:How to check if the result of division is int or float如何检查除法的结果是int还是float
【发布时间】:2017-01-08 08:43:50
【问题描述】:

我正在生成 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);
}

【问题讨论】:

  • 使用TypeVariable..
  • 为什么停止代码工作。给它一个例外,您可以将其添加到您的问题中吗?请阅读how to ask
  • H. Pauwelyn 我不明白你在说什么。

标签: android math int operators


【解决方案1】:
if (first % twond == 0) {
    // result is an integer
}

【讨论】:

  • 我正在尝试:while (first % twond != 0) { } 因为我想重新生成第一个和第二个以获得整数结果。但它仍然无法正常工作。
  • @Fachri ...你为什么不让它变得更简单,只生成一个整数,然后将它乘以另一个生成的整数?
  • Karakuri@ 我想做一个数学测验,我需要第一个整数和第二个整数。
猜你喜欢
  • 2011-05-31
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
相关资源
最近更新 更多