如果标题有点令人困惑,我很抱歉,我在尝试表达我想说的内容时遇到了一些困难。
这是我的代码:
public class Assignment3 {
public static void main(String[] args) {
int A = 100;
int B = 20;
int C = 30;
int X = 4 * A;
int Y = 3 * (B + C);
int Z = X / Y;
System.out.println("The value of A is " + A + ".");
System.out.println("The value of B is " + B + ".");
System.out.println("The value of C is " + C + ".");
System.out.println("The answer is " + Z + ".");
}
}
当我知道答案是 2.6 重复时,它一直给我 2 作为答案。有什么想法以及如何修复它吗?提前致谢。
最佳答案
X 和 Y 都是整数(非十进制值)。如果执行整数除法,小数点会被截断,从而导致意外的结果。
关于java - 为什么我的代码给出了这个数学问题的错误答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38155604/