2015-08-25 75 views
-1

我试图比较功能。每个函数都有这个:return returnArr.toString();作为回报,它们都是字符串。我的比较似乎是错误的

if (moveRight() == paintFood(235,10,"blue")){ 
alert("Food!") 
}; 

但它是不一样的像这样的:

if ("A" == "A"){ 
alert("Right comparison"); 
}; 

为什么???

+0

向我们展示这两种方法的代码。 – csmckelvey

+0

使用“.equals()”函数。 参见此:http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – mfruizs2

+0

不健全的优越感,但(一致)[缩进](些许的HTTPS ://en.wikipedia.org/wiki/Indent_style)不会误入歧途。 – Jubobs

回答

0
new String("a") == new String("a") 

评估为假。

尝试

if (moveRight().valueOf() == paintFood(235,10,"blue").valueOf()){ 

代替。

0

感谢您的帮助。 问题同时解决。如果条件放在一个函数中运行它是否需要,如果条件只是不正确的地方:

function check(){ 
if (moveRight() == paintFood(235,10,"blue")){ 
alert("Food!") 
}; 
};