Java 确定序列是否为真

Java 确定序列是否为真,java,Java,如何确定序列是否为真? 假设我有这个代码 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)是否处于所述的序列中?您可以检查两个数字之间的距离是否等于一: if(b-a == 1 && c-b == 1) 将数组填充为序列(范围)并与原始值(数组

如何确定序列是否为真? 假设我有这个代码

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)是否处于所述的序列中?

您可以检查两个数字之间的距离是否等于一:

if(b-a == 1 && c-b == 1)

将数组填充为序列(范围)并与原始值(数组)进行比较

public static boolean checker() {
    // you can do this without worrying about overflow
    // because nextInt method generates a number btw 0(inclusive) and 10(exclusive)
    return (b-a == 1 && c-b == 1) || (b-a == -1 && c-b == -1);
    //            1, 2, 3         or          3, 2, 1
}

根据OP的描述,我认为(5,4,3)也应该计算为true,这是无法处理的。我认为(3,1,2)也应该是正确的。我不确定(5,5,5)。是的,基本上是1,2,3或4,5,6的顺序,等等。不是5,5,5或7,7,7你需要更精确地知道你想要什么。当(a=5,b=4,c=3)时会发生什么——这应该是真的还是假的?(a=3,b=1,c=2)呢?你应该多考虑一下什么条件是更精确的——这也可以给你一个关于如何计算它们的线索。
public static boolean checker() {
    // you can do this without worrying about overflow
    // because nextInt method generates a number btw 0(inclusive) and 10(exclusive)
    return (b-a == 1 && c-b == 1) || (b-a == -1 && c-b == -1);
    //            1, 2, 3         or          3, 2, 1
}