Java:检查数字是否在特定范围内

Java:检查数字是否在特定范围内,java,numbers,range,Java,Numbers,Range,我有一个包含五个值的整数数组,按升序排序,没有重复项。我想检查数组中的五个值中是否有三个符合要求,即它们在一个序列中,但不必全部符合要求 如果以下数字组中的三个在五个数字的数组中,则满足要求: (1,2,3,4) 或 (5,6,7,8) 或 (9,10,11,12) 等等 因此,以下数组返回true: 示例: (1,2,3,18,40) 或 (2,3,4,20,34) 或 (1,3,4,7,12) 或 (9,11,12,31,51) 而以下数组返回false: 示例: (3,4,5,11,29)

我有一个包含五个值的整数数组,按升序排序,没有重复项。我想检查数组中的五个值中是否有三个符合要求,即它们在一个序列中,但不必全部符合要求

如果以下数字组中的三个在五个数字的数组中,则满足要求:

(1,2,3,4)
(5,6,7,8)
(9,10,11,12)
等等

因此,以下数组返回true:

示例:
(1,2,3,18,40)
(2,3,4,20,34)
(1,3,4,7,12)
(9,11,12,31,51)

而以下数组返回false:

示例:
(3,4,5,11,29)
(15、16、17、33、42)
等等

“规则”是五个数字组成的数组中的三个数字必须在各自的范围内,1、2、3、4或5、6、7、8等等,但我不知道如何处理这个问题

有人能帮我吗

我将永远感激你

编辑(因为作为一个新用户,我不能这么快发布我自己问题的答案):

好的,我找到了一个部分答案:首先我检查三张牌是否有相同的等级

我检查卡1到卡3是否具有相同的等级。如果不是,我检查卡2到卡4是否有相同的等级。如果没有,我会检查3到5张牌是否有相同的等级。这是我的密码:

// check
if (tempArray[0] == (tempArray[1] - 1)) {
    System.out.println("1. and 2. card match");
    if (tempArray[1] == (tempArray[2] - 1)) {
        System.out.println("2. and 3. card match");
        // three cards of same rank, starting from the first
        if (...) {
            isThreeOfAKind = true;
        }
    }
} else {
    System.out.println("1. and 2. card DO NOT match");
    // toak could start from the second card
    if (tempArray[1] == (tempArray[2] - 1)) {
        System.out.println("2. and 3. card match");
        if (tempArray[2] == (tempArray[3] - 1)) {
            System.out.println("3. and 4. card match");
            // three cards of same rank, starting from the second
            if (...) {
                isThreeOfAKind = true;
            }
        }
    } else {
        // toak could start from the third card
        System.out.println("2. and 3. card DO NOT match");
        if (tempArray[2] == (tempArray[3] - 1)) {
            System.out.println("3. and 4. card match");
            if (tempArray[3] == (tempArray[4] - 1)) {
                System.out.println("4. and 5. card match");
                // three cards of same rank, starting from the third
                if (...) {
                    isThreeOfAKind = true;
                }
            }
        }
    }
}
现在你注意到我遗漏了if子句。在那里,我必须检查数字是否在范围内(1-4、5-8、9-12等),但我不知道。!但是代码仍然有错误,因为示例1、3、4没有被识别为相同的列组,即使它是有效的

public boolean check3InARow(int[]数组){
public boolean check3InARow(int[] array) {
  int currentResult = (array[0] - 1) / 4;
  int countCurrentResult = 1;
  for (int i=1; i < array.length; i++) {
    if (((array[i] - 1) / 4) == currentResult) {
      countCurrentResult++
      if (countCurrentResult == 3) return true;
    } else {
      currentResult = (array[i] - 1) / 4;
      countCurrentResult = 1
    }
  }
  return false;
}
int currentResult=(数组[0]-1)/4; int countCurrentResult=1; for(int i=1;i
以下是我的解决方案-我认为这会很有帮助:

    public class CheckConsecutivity {

        public static boolean checkConsecutivity(int[] array) {
          final int NUMBER_OF_CONSECUTIVE_FOLLOWERS = 3; // this constant make the method general to check if any number have
                                                    // any number of consecutive followers; just change to the number you wish 4 or 10 e.g. 
          int count = 1;
          int currentPos = 0;
          for (int i = 1; i < array.length; i++) {
            if (array[currentPos] == array[i]-count) {
                System.out.println("Current Value = " + array[currentPos] + "; next value = "+array[i]  + "; count = " + count);
                count++;
                if ( count % (NUMBER_OF_CONSECUTIVE_FOLLOWERS + 1) == 0) {
                    System.out.println(NUMBER_OF_CONSECUTIVE_FOLLOWERS+" consecutive followers of "+array[currentPos]+" have been found :)");
                    return true;
                }
            } else {
                count = 1;
                i = currentPos++ + 1;
            }
          }
          System.out.println("consecutive numbers have not been found :(");
          return false;
        }

        public static void main(String[] args) {
          // TODO Auto-generated method stub
          int[] test = {7, 15, 10, 11, 12, 13, 1, 3, 4};
          int[] test1 = {4, 3, 2, 1};
          int[] test2 = {100, 101, 102, 17, 12, 1, 2, 5, 6, 7};

          System.out.println("\nTest evaluated to true++++++++++++++++++++++++++++++++:)"); 
          checkConsecutivity(test);

          System.out.println("\nTest1 evaluated to false------------------------------:("); 
          checkConsecutivity(test1);

          System.out.println("\nTest2 evaluated to false---------------------------------:(");  
          checkConsecutivity(test2);
        }

    }
公共类检查连续性{
公共静态布尔校验连续性(int[]数组){
final int NUMBER_OF_continuous_FOLLOWERS=3;//此常量使方法通用,以检查是否有任何数字
//任意数量的连续追随者;只需更改为您希望的数字4或10,例如。
整数计数=1;
int currentPos=0;
for(int i=1;i
首先用文字描述解决问题的步骤(即使用您最熟悉的自然语言)。如果你能做到这一点,那么翻译成Java就变得容易多了。如果你试图为家庭作业找到答案,你就不会成为一名更好的Java程序员。请使用@Code Guru的建议。聪明思考,如果你解决了这个问题,你会感觉很好!:-)是的,我现在就试着去做。顺便说一下,这不是什么家庭作业之类的,只是为了我正在做的一个小扑克游戏:)(三种)我现在就去做!很抱歉,您的解决方案在1,3,4,5,6中不起作用(根据你的问题,1,3,4的结果应该是真的。请检查我下面的答案,其中我将整数除以4;每组中的每个值都将有相同的结果,因此属于相同的分组。但是,如果是扑克游戏,我会正确地对卡进行建模,每个卡都有2个或更多字段,而不是将它们映射到单个字段。)t、 是的,创建一个带有“suit”和“value”字段的类,而不是使用简单的int。这也会使所有代码更具可读性。根据问题“数组按升序排序”谢谢。我试过了,但没有用。例如:13,14,15,20,48,它对13,14,15有效,吐出的值为false,但应该为true。你是对的;我在内部if(在循环中)中遗漏了一组parenthtesis.我已经修好了。谢谢你。我永远不会想出这样一个函数!我现在会检查代码并试着理解它。非常感谢!!!!!