Java 在这种情况下(模数和加法后)是否需要检查零?

Java 在这种情况下(模数和加法后)是否需要检查零?,java,collections,int,modulo,Java,Collections,Int,Modulo,如果我有例如int size并包含例如列表的大小,然后有int distance则mod操作,即distance%size为(-size-1),如果大小为5,距离为0,如果大小为5,距离为0,则在进行检查时,将永远不会进行加法,距离将为零。如果距离==n*size(n一个int),则距离%size==0。例如,如果distance==0或distance==size如果distance==n*size(n一个整数),则distance%size==0。例如,如果distance==0或dista

如果我有例如
int size
并包含例如列表的大小,然后有
int distance
mod
操作,即
distance%size
(-size-1),如果大小为5,距离为0,如果大小为5,距离为0,则在进行检查时,将永远不会进行加法,距离将为零。

如果距离==n*size
(n一个int),则
距离%size==0
。例如,如果
distance==0
distance==size

如果
distance==n*size
(n一个整数),则
distance%size==0
。例如,如果
distance==0
distance==size

那么,就有
if(distance<0)
但是距离也可以是
0
,因此跳过
距离+=大小。因此检查
距离==0

如果
,也可以使用
else编写代码:

//if distance is < 0, distance + size can't be 0 (due to the modulo before)
//however, distance could be 0 at this point if distance was 0 before or became 0 due to the modulo
if (distance < 0)  
   distance += size;  
else if (distance == 0)  //Why this check????
   return; 
//如果距离小于0,则距离+大小不能为0(由于之前的模)
//但是,如果距离在之前为0,或者由于模的原因变为0,则此时的距离可能为0
如果(距离<0)
距离+=大小;
else if(distance==0)//为什么要进行此检查????
返回;

嗯,如果(距离<0)
存在
,但是距离也可能
0
,因此跳过
距离+=大小。因此检查
距离==0

如果
,也可以使用
else编写代码:

//if distance is < 0, distance + size can't be 0 (due to the modulo before)
//however, distance could be 0 at this point if distance was 0 before or became 0 due to the modulo
if (distance < 0)  
   distance += size;  
else if (distance == 0)  //Why this check????
   return; 
//如果距离小于0,则距离+大小不能为0(由于之前的模)
//但是,如果距离在之前为0,或者由于模的原因变为0,则此时的距离可能为0
如果(距离<0)
距离+=大小;
else if(distance==0)//为什么要进行此检查????
返回;

在这里,我以为我对所有奇怪的答案都快发疯了。很明显,这里的距离很容易为0。+1如果
distance==n*size
其中n可以是负数或正数,则模数为0。在这里,我想我对所有奇怪的答案都快发疯了。很明显,这里的距离很容易为0。+1如果
distance==n*size
其中n可以为负或正,则模数为0。