Java 从五对中找出一对

Java 从五对中找出一对,java,Java,我很难在五号的一串中找到一双。因此,只能有两对。对于我找到的每一双,我应该增加2分。 这是我到目前为止得到的,但它是不正确的 String temp = "4 5 4 3 3"; String tempLine = temp.replaceAll(" ", ""); String[] hand = temp.split(" "); for(int i = 0; i < hand.length; i++) {

我很难在五号的一串中找到一双。因此,只能有两对。对于我找到的每一双,我应该增加2分。 这是我到目前为止得到的,但它是不正确的

  String  temp = "4 5 4 3 3";
  String  tempLine = temp.replaceAll(" ", "");
  String[]  hand = temp.split(" ");
        for(int i = 0; i < hand.length; i++)
                  {

                      if(hand[i].equals(tempLine.substring(0, 1)) && i !=0 )
                          score += 1;
                      if(hand[i].equals(tempLine.substring(1, 2)) && i != 1 )
                          score += 1;
                      if(hand[i].equals(tempLine.substring(2, 3)) && i!= 2 )
                          score += 1;
                      if(hand[i].equals(tempLine.substring(3, 4)) && i!= 3)
                          score += 1;
                      if(hand[i].equals(tempLine.substring(4)) && i != 4)
                          score += 1;

                  }
String temp=“4 5 4 3”;
字符串tempLine=temp.replaceAll(“,”);
字符串[]手=温度拆分(“”);
对于(int i=0;i

编辑:我试图在手上找到具有相似值的对,例如4将是在这只手上找到的一对

首先对手进行排序,然后循环查找手[I]==hand[I-1]。请注意,您可能需要稍微聪明一点,不要两次计算3或4的集合,但这应该可以让您开始了。

首先对手进行排序,然后循环查找手[i]==手[i-1]。请注意,您可能需要稍微聪明一点,不要两次计算3或4的集合,但这应该可以让您开始了。

1)为什么不将手动数组的元素与其他元素进行比较呢?您已经完成了解释字符串和创建数组数据的工作;使用它

2) 可能有更多的元素对。在电脑前,将五个类似的物体排成一行,然后自己试一试。你应该能辨认出十对。您还应该能够识别如何查找/标记配对的模式。

1)为什么不将手动数组的元素与其他元素进行比较?您已经完成了解释字符串和创建数组数据的工作;使用它


2) 可能有更多的元素对。在电脑前,将五个类似的物体排成一行,然后自己试一试。你应该能辨认出十对。您还应该能够识别如何查找/标记配对的模式。

创建一个实际的
手牌
类,不要使用
字符串来表示您的卡。
卡片
的等级和西装适合于
枚举

class Card {
  enum Suite { ... };
  enum Rank { ... };

  private final Suite s;
  private final Rank r;

  // ...
}

class Hand {
  private Card[] cards;
  // ...
}
然后将
卡片[]
排序到
手上的
类中,这样更容易评估


查看Oracle的
enum
教程,其中有一个关于卡片的示例:

创建一个实际的
Hand
类,不要使用
字符串来表示您的卡片。
卡片
的等级和西装适合于
枚举

class Card {
  enum Suite { ... };
  enum Rank { ... };

  private final Suite s;
  private final Rank r;

  // ...
}

class Hand {
  private Card[] cards;
  // ...
}
然后将
卡片[]
排序到
手上的
类中,这样更容易评估


查看Oracle的
enum
教程,其中有一个关于卡片的示例:

我认为这适合您尝试做的事情

char[] hand = {'a','b','c','b','c'};
/* sort the hand to ensure pairs are next to each other */
for(int x=0;x<hand.length - 1;x++){
    for(int y=(x+1);y<hand.length;y++){
        if((int)hand[x] > (int)hand[y]){
            char temp = hand[y];
            hand[y] = hand[x];
            hand[x] = temp;
        }
    }
}
int score = 0;
for(int x=0;x<hand.length - 1;x++){
    if(hand[x] == hand[x + 1]){
        score++;
        /*if you want to make sure you only get pairs
          add an "x++;" that way it'll skip over the 
          letter you just tested*/
    }
}
char[]hand={'a','b','c','b','c'};
/*对手进行分类,确保成对的手彼此相邻*/

对于(intx=0;x,我认为这符合您想要做的

char[] hand = {'a','b','c','b','c'};
/* sort the hand to ensure pairs are next to each other */
for(int x=0;x<hand.length - 1;x++){
    for(int y=(x+1);y<hand.length;y++){
        if((int)hand[x] > (int)hand[y]){
            char temp = hand[y];
            hand[y] = hand[x];
            hand[x] = temp;
        }
    }
}
int score = 0;
for(int x=0;x<hand.length - 1;x++){
    if(hand[x] == hand[x + 1]){
        score++;
        /*if you want to make sure you only get pairs
          add an "x++;" that way it'll skip over the 
          letter you just tested*/
    }
}
char[]hand={'a','b','c','b','c'};
/*对手进行分类,确保成对的手彼此相邻*/

对于(int x=0;x下面的示例是如何计算对的一种可能性。问题是,如果字符串或数组中有三个相等的值,它应该如何表现(增加分数)。是否将其计算为三对

123 is in fact three combinations 12 && 13 && 23 111 is in fact three combinations 11 && 11 && 11 123实际上是三个组合12&&13&&23 111实际上是三个组合11&&11&&11

包edu.harris.pairs;
公共类搜索引擎{
公共静态void main(字符串[]args){
字符串s=“hello”;
智力得分=0;
对于(int i=0;i

希望这能有所帮助。

下面的示例是如何计算对的一种可能性。问题是,如果字符串或数组中有三个相等的值,它应该如何表现(增加分数)。这应该算为三对还是根本不算

123 is in fact three combinations 12 && 13 && 23 111 is in fact three combinations 11 && 11 && 11 123实际上是三个组合12&&13&&23 111实际上是三个组合11&&11&&11

包edu.harris.pairs;
公共类搜索引擎{
公共静态void main(字符串[]args){
字符串s=“hello”;
智力得分=0;
对于(int i=0;i

希望这能有所帮助。

我正在尝试在手上找到具有类似值的对,例如4将是在这只手上找到的一对…并且?您的数组在两个不同的空格中包含4。将数组的一个元素与数组的另一个元素进行比较。不要将其与字符串的一部分进行比较。我正在尝试在手上找到具有相同值的对类似的值,例如4是在这只手上找到的一对…和?你的数组在两个不同的空间中包含4。将数组的一个元素与数组的另一个元素进行比较。不要将它与字符串的一部分进行比较。你能再给一些代码吗?以及你试图完成的任务的一些解释吗?你能再给一些代码吗?和更多的解释你想要完成的事情!