如何确定序列是否正确? 假设我有这段代码
int a = random.nextInt(10);
int b = random.nextInt(10);
int c = random.nextInt(10);
如何使用 if 语句或循环来确定是否/何时 a = 1、b = 2 和 c = 3(或 a、b 和 c 的任意组合仅相隔一个数字。例如 4, 5,6 或 7,8,9) 的顺序是否如所述?
最佳答案
您可以检查两个数字之间的距离是否等于 1:
if(b-a == 1 && c-b == 1)
关于java - 确定序列是否为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400647/