简体   繁体   English

如何告诉我的代码“ Flush”?

[英]How can I tell my code that it has a “Flush”?

I'm suppose to create a code that recognizes if my hand has the same card faces 我想创建一个代码来识别我的手是否具有相同的卡面

public static boolean sameFace(String hand) {

hand = "s9s7s2sQsK";
char f = hand.charAt(0);

if( hand.charAt(0)==hand.charAt(2) && hand.charAt(0)==hand.charAt(4) 
&& hand.charAt(0)==hand.charAt(6) && hand.charAt(0)==hand.charAt(8));

return (hand.charAt(0) == hand.charAt(2) && hand.charAt(0) == hand.charAt(4) 
   && hand.charAt(0) == hand.charAt(6) && hand.charAt(0) == hand.charAt(8));

   sameface = hand;
   if (hand==true;)

   return (hand==true;) ; 

 } 

As can be seen above, if all positions are the same characters, it comes true(False, if even one isn't the same.) How can I then use the result of that "return" to let my program recognize it has the same faces or not? 从上面可以看出,如果所有位置都是相同的字符,那么它就为真(如果甚至不相同,则为False。)然后我如何使用该“返回”结果让我的程序识别出它具有是否有相同的面孔? If that is even possible. 如果可能的话。

From what i know, based on my code, it's saying "Yes, positions x=y=z are the same" how can I then tell it "Since they are the same, they have the same card faces." 据我所知,根据我的代码,它说“是的,位置x = y = z是相同的”,然后我如何才能告诉它“由于它们相同,所以它们具有相同的牌面”。

I tried to put this at the end 我试图把这个放在最后

   sameface = hand;
   if (hand==true;)
       return (hand==true;) ;  

Basically I'm trying to say that when the "hand" return statement is true, then samefaces is true. 基本上,我想说的是,当“ hand” return语句为true时,samefaces为true。 Meaning that the faces are the same. 这意味着面孔是相同的。 And if it's false it'll return false. 如果为假,它将返回假。

Basically I'm trying to say that when the "hand" return statement is true, then samefaces is true. 基本上,我想说的是,当“ hand” return语句为true时,samefaces为true。 Meaning that the faces are the same. 这意味着面孔是相同的。 And if it's false it'll return false. 如果为假,它将返回假。

You do that simply by returning the result of the expression: 您只需返回表达式的结果即可:

public static boolean sameFace(String hand) {
    char f = hand.charAt(0);
    return f == hand.charAt(2) &&
           f == hand.charAt(4) &&
           f == hand.charAt(6) &&
           f == hand.charAt(8);
}

Or if you want to be friendly to a different number of cards, use a loop: 或者,如果您想对其他数量的卡片友好,请使用循环:

public static boolean sameFace(String hand) {
    char f = hand.charAt(0);
    for (int i = 2, len = hand.length(); i < len; i += 2) {
        if (f != hand.charAt(i)) {
            // Not a match
            return false;
        }
    }

    // All matched
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM