Java I';我正在检查某些坐标是否相同,但失败了,我不知道为什么

Java I';我正在检查某些坐标是否相同,但失败了,我不知道为什么,java,Java,使用Java开发插件。一切正常。我来到这个if语句,它就是失败了。我已经尝试过将值从int改为string,反之亦然,它根本不会移动。我记录了坐标。我尝试将它们作为字符串进行比较。 代码: 配置: 控制台日志: -226 [11:31:47 INFO]: -226 [11:31:47 INFO]: 64 [11:31:47 INFO]: 64 [11:31:47 INFO]: 266 [11:31:47 INFO]: 266 [11:31:47 INFO]: ERROR: Failed on

使用Java开发插件。一切正常。我来到这个if语句,它就是失败了。我已经尝试过将值从int改为string,反之亦然,它根本不会移动。我记录了坐标。我尝试将它们作为字符串进行比较。
代码:


配置:

控制台日志:

-226
[11:31:47 INFO]: -226
[11:31:47 INFO]: 64
[11:31:47 INFO]: 64
[11:31:47 INFO]: 266
[11:31:47 INFO]: 266
[11:31:47 INFO]: ERROR: Failed on the right click coordinate check.

我希望任何人都能帮助我。

您不能将2个字符串与==进行比较,因为字符串是类,==检查同一实例,您必须使用String.equals(OtherString)或将它们转换为整数,如下所示:

int locationx = Integer.parsInt(plugin.getConfig().getString("locationx"));
int eventlocationx = event.getClickedBlock().getX();

int locationy = Integer.parsInt(plugin.getConfig().getString("locationy"));
int eventlocationy = event.getClickedBlock().getY();

int locationz = Integer.parsInt(plugin.getConfig().getString("locationz"));
int eventlocationz = event.getClickedBlock().getZ();

if (locationx == eventlocationx && locationy == eventlocationy && locationz == 
eventlocationz)
{
System.out.println("SUCCESS: Passed coordinate check on the right click.");
 return true;
}
else
{
System.out.println(locationx);
System.out.println(eventlocationx);

System.out.println(locationy);
System.out.println(eventlocationy);

System.out.println(locationz);
System.out.println(eventlocationz);

System.out.println("ERROR: Failed on the right click coordinate check.");
return false;
}

将从配置中获得的字符串转换为整数,然后进行比较,这比您现在尝试的将整数转换为字符串并进行比较要好。您的字符串比较,正如副本所说,是错误的,但最好从int开始进行比较。非常感谢您的回答。我想我应该做更多的研究。
-226
[11:31:47 INFO]: -226
[11:31:47 INFO]: 64
[11:31:47 INFO]: 64
[11:31:47 INFO]: 266
[11:31:47 INFO]: 266
[11:31:47 INFO]: ERROR: Failed on the right click coordinate check.
int locationx = Integer.parsInt(plugin.getConfig().getString("locationx"));
int eventlocationx = event.getClickedBlock().getX();

int locationy = Integer.parsInt(plugin.getConfig().getString("locationy"));
int eventlocationy = event.getClickedBlock().getY();

int locationz = Integer.parsInt(plugin.getConfig().getString("locationz"));
int eventlocationz = event.getClickedBlock().getZ();

if (locationx == eventlocationx && locationy == eventlocationy && locationz == 
eventlocationz)
{
System.out.println("SUCCESS: Passed coordinate check on the right click.");
 return true;
}
else
{
System.out.println(locationx);
System.out.println(eventlocationx);

System.out.println(locationy);
System.out.println(eventlocationy);

System.out.println(locationz);
System.out.println(eventlocationz);

System.out.println("ERROR: Failed on the right click coordinate check.");
return false;
}