如何使用Java确定我在数组中键入的5个数字是否是连续的?

如何使用Java确定我在数组中键入的5个数字是否是连续的?,java,arrays,methods,Java,Arrays,Methods,我想写一个非常简单的扑克游戏。我只是用非正面卡,2-9,没有套间之类的。我试图找出如何编写一个方法,确定五张卡片是否是一个满屋子,这是一对和一种3。我让用户输入5个表示卡值的整数,并将它们存储在单个数组中。我试着这样写: public static boolean containsFullHouse(int[] hand) { for (int i = 0; i < hand.length; i++){ int count = 0; for

我想写一个非常简单的扑克游戏。我只是用非正面卡,2-9,没有套间之类的。我试图找出如何编写一个方法,确定五张卡片是否是一个满屋子,这是一对和一种3。我让用户输入5个表示卡值的整数,并将它们存储在单个数组中。我试着这样写:

public static boolean containsFullHouse(int[] hand)
{
     for (int i = 0; i < hand.length; i++){
         int count = 0;

         for (int j = 0; j < hand.length; j++){
              if (hand[i] == hand[j]){
                 count++;}

              if (count == 3){
                 return true;}
         }
     }
         for(int i = 0; i < hand.length; i++){
              for(int j = i + 1; j < hand.length; j++){ 
                  if(hand[i] == hand[j]){
                        return true;}
                   }
               }
         }  
    return false;
}
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
        (hand[2] == hand[1] || hand[2] == hand[3]));
public static boolean containsFullHouse(int[] hand)
{
    int value1 = -1, count1 = 0;
    int value2 = -1, count2 = 0;

    for (int i = 0; i < hand.length; i++) {
        if(hand[i] == value1) {
            // Found another value1 card
            count1++;
        } else if(hand[i] == value2) {
            // Found another value2 card
            count2++;
        } else if(value1 == -1) {
            // Found a new card, store as value1
            value1 = hand[i];
            count1++;
        } else if(value2 == -1) {
            // Found a new card, store as value2
            value2 = hand[i];
            count2++;
        } else {
            // Found a third card, so it cannot be a full house!
            return false;
        }
    }
    if(value2 == -1) {
        // Found 'five of a kind'?!
        return false;
    }

     // Check if it is a full house
    return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}
publicstaticboolearncontainsfullhouse(int[]hand)
{
对于(int i=0;i
问题:

public static boolean containsFullHouse(int[] hand)
{
     // a variable that keeps track of one of the 3-of-a-kind indices (used in 2-of-a-kind check)
     int pos = -1;

     for (int i = 0; i < hand.length && pos == -1; i++){
         // start count at one instead
         int count = 1;

         // start j from next position rather than 0
         for (int j = i+1; j < hand.length && pos == -1; j++){
              if (hand[i] == hand[j]) {
                 count++;
              }

              if (count == 3) {
                 pos = i;
              }
         }
     }

     // if we didn't find 3-of-a-kind, return false
     if (pos == -1)
         return false;

     // look for 2-of-a-kind
     for(int i = 0; i < hand.length; i++){
         // exclude elements that match one of the 3-of-a-kind
         if (hand[i] != hand[pos]){
             for(int j = i + 1; j < hand.length; j++){ 
                 if(hand[i] == hand[j]){
                        return true;
                 }
             }
         }
     }

    return false;
}
  • 您检查了索引
    i
    两次,虽然正确(因为您检查了
    count==3
    ),但这是不必要的
  • 在你检查另外两个之前,你也要回来
  • 第二个循环将返回true,因为它将查找前一个循环中的数字
如果您对它们进行排序,只需检查两侧的两对卡片是否相同,以及中间的卡片是否与其中一张相同即可。比如说:

public static boolean containsFullHouse(int[] hand)
{
     for (int i = 0; i < hand.length; i++){
         int count = 0;

         for (int j = 0; j < hand.length; j++){
              if (hand[i] == hand[j]){
                 count++;}

              if (count == 3){
                 return true;}
         }
     }
         for(int i = 0; i < hand.length; i++){
              for(int j = i + 1; j < hand.length; j++){ 
                  if(hand[i] == hand[j]){
                        return true;}
                   }
               }
         }  
    return false;
}
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
        (hand[2] == hand[1] || hand[2] == hand[3]));
public static boolean containsFullHouse(int[] hand)
{
    int value1 = -1, count1 = 0;
    int value2 = -1, count2 = 0;

    for (int i = 0; i < hand.length; i++) {
        if(hand[i] == value1) {
            // Found another value1 card
            count1++;
        } else if(hand[i] == value2) {
            // Found another value2 card
            count2++;
        } else if(value1 == -1) {
            // Found a new card, store as value1
            value1 = hand[i];
            count1++;
        } else if(value2 == -1) {
            // Found a new card, store as value2
            value2 = hand[i];
            count2++;
        } else {
            // Found a third card, so it cannot be a full house!
            return false;
        }
    }
    if(value2 == -1) {
        // Found 'five of a kind'?!
        return false;
    }

     // Check if it is a full house
    return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}
或者,如果要修复函数:

public static boolean containsFullHouse(int[] hand)
{
     // a variable that keeps track of one of the 3-of-a-kind indices (used in 2-of-a-kind check)
     int pos = -1;

     for (int i = 0; i < hand.length && pos == -1; i++){
         // start count at one instead
         int count = 1;

         // start j from next position rather than 0
         for (int j = i+1; j < hand.length && pos == -1; j++){
              if (hand[i] == hand[j]) {
                 count++;
              }

              if (count == 3) {
                 pos = i;
              }
         }
     }

     // if we didn't find 3-of-a-kind, return false
     if (pos == -1)
         return false;

     // look for 2-of-a-kind
     for(int i = 0; i < hand.length; i++){
         // exclude elements that match one of the 3-of-a-kind
         if (hand[i] != hand[pos]){
             for(int j = i + 1; j < hand.length; j++){ 
                 if(hand[i] == hand[j]){
                        return true;
                 }
             }
         }
     }

    return false;
}
publicstaticboolearncontainsfullhouse(int[]hand)
{
//跟踪3类索引之一的变量(用于2类检查)
int pos=-1;
对于(int i=0;i
问题:

public static boolean containsFullHouse(int[] hand)
{
     // a variable that keeps track of one of the 3-of-a-kind indices (used in 2-of-a-kind check)
     int pos = -1;

     for (int i = 0; i < hand.length && pos == -1; i++){
         // start count at one instead
         int count = 1;

         // start j from next position rather than 0
         for (int j = i+1; j < hand.length && pos == -1; j++){
              if (hand[i] == hand[j]) {
                 count++;
              }

              if (count == 3) {
                 pos = i;
              }
         }
     }

     // if we didn't find 3-of-a-kind, return false
     if (pos == -1)
         return false;

     // look for 2-of-a-kind
     for(int i = 0; i < hand.length; i++){
         // exclude elements that match one of the 3-of-a-kind
         if (hand[i] != hand[pos]){
             for(int j = i + 1; j < hand.length; j++){ 
                 if(hand[i] == hand[j]){
                        return true;
                 }
             }
         }
     }

    return false;
}
  • 您检查了索引
    i
    两次,虽然正确(因为您检查了
    count==3
    ),但这是不必要的
  • 在你检查另外两个之前,你也要回来
  • 第二个循环将返回true,因为它将查找前一个循环中的数字
如果您对它们进行排序,只需检查两侧的两对卡片是否相同,以及中间的卡片是否与其中一张相同即可。比如说:

public static boolean containsFullHouse(int[] hand)
{
     for (int i = 0; i < hand.length; i++){
         int count = 0;

         for (int j = 0; j < hand.length; j++){
              if (hand[i] == hand[j]){
                 count++;}

              if (count == 3){
                 return true;}
         }
     }
         for(int i = 0; i < hand.length; i++){
              for(int j = i + 1; j < hand.length; j++){ 
                  if(hand[i] == hand[j]){
                        return true;}
                   }
               }
         }  
    return false;
}
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
        (hand[2] == hand[1] || hand[2] == hand[3]));
public static boolean containsFullHouse(int[] hand)
{
    int value1 = -1, count1 = 0;
    int value2 = -1, count2 = 0;

    for (int i = 0; i < hand.length; i++) {
        if(hand[i] == value1) {
            // Found another value1 card
            count1++;
        } else if(hand[i] == value2) {
            // Found another value2 card
            count2++;
        } else if(value1 == -1) {
            // Found a new card, store as value1
            value1 = hand[i];
            count1++;
        } else if(value2 == -1) {
            // Found a new card, store as value2
            value2 = hand[i];
            count2++;
        } else {
            // Found a third card, so it cannot be a full house!
            return false;
        }
    }
    if(value2 == -1) {
        // Found 'five of a kind'?!
        return false;
    }

     // Check if it is a full house
    return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}
或者,如果要修复函数:

public static boolean containsFullHouse(int[] hand)
{
     // a variable that keeps track of one of the 3-of-a-kind indices (used in 2-of-a-kind check)
     int pos = -1;

     for (int i = 0; i < hand.length && pos == -1; i++){
         // start count at one instead
         int count = 1;

         // start j from next position rather than 0
         for (int j = i+1; j < hand.length && pos == -1; j++){
              if (hand[i] == hand[j]) {
                 count++;
              }

              if (count == 3) {
                 pos = i;
              }
         }
     }

     // if we didn't find 3-of-a-kind, return false
     if (pos == -1)
         return false;

     // look for 2-of-a-kind
     for(int i = 0; i < hand.length; i++){
         // exclude elements that match one of the 3-of-a-kind
         if (hand[i] != hand[pos]){
             for(int j = i + 1; j < hand.length; j++){ 
                 if(hand[i] == hand[j]){
                        return true;
                 }
             }
         }
     }

    return false;
}
publicstaticboolearncontainsfullhouse(int[]hand)
{
//跟踪3类索引之一的变量(用于2类检查)
int pos=-1;
对于(int i=0;i
一个完整的房子由两个不同的整数组成,因此为这两个整数保留一个计数器。它还需要跟踪两个不同的值。如果你把这些结合起来,你会得到这样的结果:

public static boolean containsFullHouse(int[] hand)
{
     for (int i = 0; i < hand.length; i++){
         int count = 0;

         for (int j = 0; j < hand.length; j++){
              if (hand[i] == hand[j]){
                 count++;}

              if (count == 3){
                 return true;}
         }
     }
         for(int i = 0; i < hand.length; i++){
              for(int j = i + 1; j < hand.length; j++){ 
                  if(hand[i] == hand[j]){
                        return true;}
                   }
               }
         }  
    return false;
}
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
        (hand[2] == hand[1] || hand[2] == hand[3]));
public static boolean containsFullHouse(int[] hand)
{
    int value1 = -1, count1 = 0;
    int value2 = -1, count2 = 0;

    for (int i = 0; i < hand.length; i++) {
        if(hand[i] == value1) {
            // Found another value1 card
            count1++;
        } else if(hand[i] == value2) {
            // Found another value2 card
            count2++;
        } else if(value1 == -1) {
            // Found a new card, store as value1
            value1 = hand[i];
            count1++;
        } else if(value2 == -1) {
            // Found a new card, store as value2
            value2 = hand[i];
            count2++;
        } else {
            // Found a third card, so it cannot be a full house!
            return false;
        }
    }
    if(value2 == -1) {
        // Found 'five of a kind'?!
        return false;
    }

     // Check if it is a full house
    return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}
publicstaticboolearncontainsfullhouse(int[]hand)
{
int value1=-1,count1=0;
int value2=-1,count2=0;
对于(int i=0;i
一个完整的房子由两个不同的整数组成,因此为这两个整数保留一个计数器。它还需要跟踪两个不同的值。如果你把这些结合起来,你会得到这样的结果:

public static boolean containsFullHouse(int[] hand)
{
     for (int i = 0; i < hand.length; i++){
         int count = 0;

         for (int j = 0; j < hand.length; j++){
              if (hand[i] == hand[j]){
                 count++;}

              if (count == 3){
                 return true;}
         }
     }
         for(int i = 0; i < hand.length; i++){
              for(int j = i + 1; j < hand.length; j++){ 
                  if(hand[i] == hand[j]){
                        return true;}
                   }
               }
         }  
    return false;
}
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
        (hand[2] == hand[1] || hand[2] == hand[3]));
public static boolean containsFullHouse(int[] hand)
{
    int value1 = -1, count1 = 0;
    int value2 = -1, count2 = 0;

    for (int i = 0; i < hand.length; i++) {
        if(hand[i] == value1) {
            // Found another value1 card
            count1++;
        } else if(hand[i] == value2) {
            // Found another value2 card
            count2++;
        } else if(value1 == -1) {
            // Found a new card, store as value1
            value1 = hand[i];
            count1++;
        } else if(value2 == -1) {
            // Found a new card, store as value2
            value2 = hand[i];
            count2++;
        } else {
            // Found a third card, so it cannot be a full house!
            return false;
        }
    }
    if(value2 == -1) {
        // Found 'five of a kind'?!
        return false;
    }

     // Check if it is a full house
    return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}
publicstaticboolearncontainsfullhouse(int[]hand)
{
int value1=-1,count1=0;
int value2=-1,count2=0;
对于(int i=0;i