简体   繁体   English

为什么这两个代码片段不同

[英]Why do these two code fragments differ

I just noticed that when I change the last line in the code fragment from potential =+ rep_pot to potential = potential + rep_pot , I get completely different behaviour. 我只是注意到,当我将代码片段的最后一行从potential =+ rep_potpotential = potential + rep_pot ,我得到了完全不同的行为。 Has anybody got any idea why this is happening? 有人知道为什么会这样吗?

double potential = euclideanDistance(i, goal);
for (IntPoint h: hits){
    double dist = euclideanDistance(i, h);
    double a = range - dist;
    double rep_pot = (Math.exp(-1/a)) / dist;
    potential =+ rep_pot;
}

There is no =+ operator in Java. Java中没有=+运算符。 See the Java Language Specification for all legal operators. 有关所有合法运算符,请参见Java语言规范

=+ are two operators: = followed by + . =+两个运算符: =后跟+

That is because 那是因为

potential = potential + rep_pot

is similar to 类似于

potential += rep_pot

and

potential =+ rep_pot;

is the same as 是相同的

potential = rep_pot;

You probably meant += . 您可能是说+= In your case it is interpreted as x = +x which is x = x 在您的情况下,它被解释为x = +x ,即x = x

Use += : 使用+=

potential += rep_pot;

Yes, because these two things are not equivalent. 是的,因为这两件事并不等同。

potential =+ rep_pot;

Here we have potential assigned a value of the expression 'unary plus rep_pot' 在这里,我们可能为表达式分配了一个值'unary plus rep_pot'

The thing you intendet to write looks differently: 您打算写的东西看起来有所不同:

potential += rep_pot;

And this is equivalent to 这相当于

potential = potential + rep_pot;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么两个IP不同? - why the two IP differ? 这两个代码片段是相同的还是它们有何不同? - Are these two code snippets the same or do they differ in any way? 您如何检查两个提交是否仅在代码格式上有所不同 - How do you check if two commits differ only in code formatting 为什么这两种将GUI元素与Action相关联的方式在结果上有所不同? - Why do these two ways of associating a GUI element with an Action differ in their results? 这两个代码片段中哪个更好 - Which is the better of these two code fragments 如何用Java 8函数样式重写仅在一个变量类型上不同的两个代码分支? - How do I rewrite two branches of code that only differ in one variable's type, in a Java 8 functional style? 为什么两个浮点结果在名义上具有相同的值时会有所不同? - Why do two floating-point results differ when they nominally have the same value? 当我使用NetBeans 6.8和Eclipse运行此代码时,输​​出为何不同? - Why do the outputs differ when I run this code using NetBeans 6.8 and Eclipse? 我如何将两个活动从我的代码转换为两个片段 - How do i convert the two activities from my code to two Fragments 构造函数中的虚函数,为什么语言不同? - Virtual functions in constructors, why do languages differ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM