java 如何在Java中完成加减运算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12699809/
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
How to complete a plus-minus operation in Java?
提问by matt-v-from-toronto
Is there an operation for plus-minus in Java?
Java中是否有加减运算?
If not, then what's the most efficient way to do so?
如果没有,那么最有效的方法是什么?
Let's say you got two variables, a = 4
and b = 3
, what I want is to be able to do a ± b
which would return two values, 7 and 1, because 4 + 3 is 7 and 4 - 3 is 1.
假设您有两个变量,a = 4
并且b = 3
,我想要的是能够a ± b
返回两个值,7 和 1,因为 4 + 3 是 7,而 4 - 3 是 1。
回答by ajon
plus-minus is not an operation, it is a symbol. If you just want to include it in a string you can use Unicode \u00B1 such as:
加减号不是一个运算,它是一个符号。如果您只想将其包含在字符串中,您可以使用 Unicode \u00B1 例如:
System.out.println("sqrt(9) = \u00B1 3");
System.out.println("sqrt(9) = \u00B1 3");
回答by Alexei Kaigorodov
Probably "interval arithmetic" is what you are looking for.
可能“区间算术”就是您正在寻找的。
回答by Andrew Cooper
If you read the Wiki article carefully you'll note that plus-minus is not an operation. It's a sign used in engineering and mathematics (amongst other fields) to indicate that a value could be positive or negative.
如果您仔细阅读 Wiki 文章,您会注意到加减不是运算。它是在工程和数学(以及其他领域)中使用的一个符号,用于表示一个值可以是正数或负数。
For example the the square root of 4 is not actually 2, it's "plus-minus 2", because (+2)^2 and (-2)^2 are both 4.
例如,4 的平方根实际上不是 2,而是“正负 2”,因为 (+2)^2 和 (-2)^2 都是 4。
It's also used in tolerances and errors in measurements such as "The part you're paking must be 100mm plus-minus 0.5mm in length" (the part must be between 99.5mm and 100.5 mm in length).
它还用于测量公差和误差,例如“您要包装的零件的长度必须为 100 毫米正负 0.5 毫米”(零件的长度必须介于 99.5 毫米和 100.5 毫米之间)。
Because variables can only have one value a plus-minus token/keyword/whatever doesn't make much sense. You could however use a data structure and associate methods appropriate to the application to keep track of these kinds of values.
因为变量只能有一个值,加减标记/关键字/任何没有多大意义。但是,您可以使用适合应用程序的数据结构和关联方法来跟踪这些类型的值。