gpt4 book ai didi

java - 我试图检查某些坐标是否相同,但失败了,我不知道为什么

转载 作者:行者123 更新时间:2023-11-30 05:40:57 25 4
gpt4 key购买 nike

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

String locationx = plugin.getConfig().getString("locationx");
String eventlocationx = Integer.toString(event.getClickedBlock().getX());

String locationy = plugin.getConfig().getString("locationy");
String eventlocationy = Integer.toString(event.getClickedBlock().getY());

String locationz = plugin.getConfig().getString("locationz");
String eventlocationz = Integer.toString(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;
}


配置:

locationx: '-226'

locationy: '64'

locationz: '266'

控制台日志:

-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.

希望大家能帮帮我。

最佳答案

你不能用 == 比较两个字符串,因为字符串是类,== 检查同一个实例,你必须使用 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;
}

关于java - 我试图检查某些坐标是否相同,但失败了,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55686501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com