gpt4 book ai didi

java - 如何获得两个整数之间的差

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:33 25 4
gpt4 key购买 nike

我正在尝试制作一个使用区域的程序,每个区域都有一个 ID(例如:1;1),我正在尝试通过比较两个 ID 来获取指定区域的大小,但此方法返回 1 作为尺寸。

 //Pos1 = -2;3 Pos2 = 0;1
int x = Integer.valueOf(pos2.x).compareTo(pos1.x);
int y = Integer.valueOf(pos2.y).compareTo(pos1.y);
int size = Math.abs(x * y);

那么我怎样才能让它发挥作用呢?

最佳答案

compareTo 不应该返回两个值之间的确切差异。来自 the docs :

Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

使用

int x = Math.abs(pos2.x-pos1.x);
int y = Math.abs(pos2.y-pos1.y);
int size = x * y;

关于java - 如何获得两个整数之间的差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38078503/

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