在下面的代码中,当 x = 60 和 y = 2 时,结果 = 500。这是正确的,但 60 到 119 之间的任何 x 值也给出 500。此外,当 x < 60 时,我得到除以 0 错误。此外,当 x >= 120 时,结果 = 0。我很难理解为什么会这样。我也尝试过使用 int、float 和 long 的各种组合,但仍然没有运气。
public class main {
static long result;
static int x;
static int y;
public static void main(String[] args) {
x = 60;
y = 2;
result = 1000 * (1 / (x / 60)) / y;
System.out.println(result);
}
}
顺便说一句,我在尝试为 Android 制作节拍器应用程序时遇到了这个问题。我把这段代码脱离了上下文,以便更容易地隔离问题。非常感谢任何帮助和/或建议!