java 6÷2(1+2) = 的答案是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5843983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:04:43  来源:igfitidea点击:

What is the answer of: 6÷2(1+2) =?

javamath

提问by Ryan S

I have this debate, whether the answer of 6÷2(1+2) is 9 or 1.

我有这个争论,6÷2(1+2)的答案是9还是1。

public class bodmas
{
public static void main(String [] args)
{
    float i = 6/2*(1+2);
    System.out.println(i);
}

}

Java returns 9, but that's because I cannot remove the * sign. If I put this into my Casio fx-82MS calculator it returns 9 if I insert 6/2*(1+2)but 1 if I insert 6/2(1+2)

Java 返回 9,但那是因为我无法删除 * 符号。如果我把它放入我的卡西欧 fx-82MS 计算器,如果我插入6/2*(1+2),它会返回 9,如果我插入6/2(1+2),它会返回1

This worries me as a Java Developer because if the answer is really different, we have a problem. I have tried it on different versions of calculators, the answer gives out 9, however on mine it gives out 1. So probably there's nothing to worry about.

作为一名 Java 开发人员,这让我很担心,因为如果答案真的不同,我们就有问题了。我在不同版本的计算器上试过,答案是 9,但我的结果是 1。所以可能没有什么可担心的。

As a Conclusion, the question is ambiguous and in programming terms it would return 9. If it had to return 1, a more clear question would be 6/(2*(1+2)) .

作为结论,这个问题是模棱两可的,在编程术语中它会返回 9。如果它必须返回 1,一个更明确的问题是 6/(2*(1+2)) 。

回答by Kevin Sylvestre

According to Order of Operationsthe answer should be 9.

根据操作顺序,答案应该是 9。

回答by Oliver Charlesworth

It depends on how you define your representation. In Java (indeed, in most languages):

这取决于您如何定义您的表示。在 Java 中(实际上,在大多数语言中):

6 / 2 * (1+2)

is equivalent to

相当于

(6 / 2) * (1+2)

which is 9.

这是 9。

Your calculator is interpreting it as:

您的计算器将其解释为:

6 / (2 * (1+2))

which is indeed 1.

这确实是 1。

回答by caper_26

omg guys. Ask youself: What is a ÷ a = ? It is ONE. Now think of Itentity Law: a = 1a = 1(a) now ask what is a ÷ 1a = ? Obviously 1 !! a ÷ 1a != a ÷ 1 * a What is a ÷ 2a ? Ans: 1/2 What is 6 ÷ 2a ? Ans: 3/a Let a = 1+2 in all of the above. Stop converting stuff like 2a = 2 * a. It is 2a = (2 * a)

天哪,伙计们。问问自己:什么是 ÷ a = ?它是一。现在想想实体定律:a = 1a = 1(a) 现在问什么是 a ÷ 1a = ?显然 1 !! a ÷ 1a != a ÷ 1 * a 什么是 ÷ 2a ?Ans: 1/2 什么是 6 ÷ 2a ?Ans: 3/a 在以上所有中设 a = 1+2。停止转换像 2a = 2 * a 这样的东西。它是 2a = (2 * a)