Java 我不明白为什么每次我试图检查所选的3张卡是否为一组时,foundCards的长度都被重置回0

Java 我不明白为什么每次我试图检查所选的3张卡是否为一组时,foundCards的长度都被重置回0,java,Java,}这是因为您在foundCards中添加了一个ArrayList,因此foundCards只包含一个ArrayList,该ArrayList本身包含3张卡。请说明如何调用此方法。我使用的是布尔值isset。isSet=playCard1.CheckSetplayCard1、playCard2、playCard3;我想知道你为什么叫isSet=playCard1.CheckSetplayCard1,playCard2,playCard3;而不是isSet=cards.CheckSetplayCar

}

这是因为您在foundCards中添加了一个ArrayList,因此foundCards只包含一个ArrayList,该ArrayList本身包含3张卡。

请说明如何调用此方法。我使用的是布尔值isset。isSet=playCard1.CheckSetplayCard1、playCard2、playCard3;我想知道你为什么叫isSet=playCard1.CheckSetplayCard1,playCard2,playCard3;而不是isSet=cards.CheckSetplayCard1、playCard2、playCard3;我从main method调用这个displayCards方法是的。但当我再次调用该方法时,我希望另一组3张卡存储在foundCards中。因此,它的长度现在将变为2。在您的checkRepeat方法中,仅当foundCard大小为0时添加一组卡,并且这只会发生一次,以便foundCards的大小最大为1。
import java.util.ArrayList;

/**
 * This class is used to create card object
 * @author Suman
 *
 */
public class Cards {

        private String num;
        private String pattern;
        private String color;
        private String shape;

        public ArrayList<ArrayList<String>> foundCards = new ArrayList<ArrayList<String>>();

        //constructors
        public Cards(String num, String pattern, String color, String shape){
            this.num=num;
            this.pattern=pattern;
            this .color=color;
            this.shape=shape;
        }

        /**
         * get methods
         * @return number of card
         */
        public String getNum(){
            return num;
        }

        /**
         * get methods
         * @return pattern of card
         */
        public String getPattern(){
            return pattern;
        }

        /**
         * get methods
         * @return color of card
         */
        public String getcolor(){
            return color;
        }

        /**
         * get methods
         * @return shape of card
         */
        public String getShape(){
            return shape;
        }

        /**
         * set method that sets number of card
         * @param num number of card
         */
        public void setNum(String num){
            this.num=num;
        }

        /**
         * set method that sets pattern of card
         * @param pattern pattern of card
         */
        public void setPattern(String pattern){
            this.pattern=pattern;
        }

        /**
         * set method that sets color of card
         * @param num color of card
         */
        public void setColor(String color){
            this.color=color;
        }

        /**
         * set method that sets shape of card
         * @param num shape of card
         */
        public void setShape(String shape){
            this.shape=shape;
        }


        /**
         * checks if the selected set of cards is valid or not
         * @param card1 is first card picked by player
         * @param card2 is second card picked by player
         * @param card3 is third card picked by player
         * @return true if given set of three cards have all same or all different property
         */

        public boolean CheckSet(Cards card1, Cards card2, Cards card3) {

            boolean isset = false;
            int matchingNoOfProperty=0;

            String checkcard1 = card1.getNum()+card1.getPattern()+card1.getcolor()+card1.getShape();
            String checkcard2 = card2.getNum()+card2.getPattern()+card2.getcolor()+card2.getShape();
            String checkcard3 = card3.getNum()+card3.getPattern()+card3.getcolor()+card3.getShape();

            //checking if user entered same card multiple times
            if (checkcard1.equals(checkcard2)){
                System.out.println("You cannot enter same card multiple times");
                return isset;
            }
            //checking if selected 3 cards has all same property or all different property
            // if all card has all same or all different property then increase matchingNoOfProporty by 1
            if ((card1.getNum().equals(card2.getNum()) && card1.getNum().equals(card3.getNum()) && card2.getNum().equals(card3.getNum())) || (!card1.getNum().equals(card2.getNum()) && !card1.getNum().equals(card3.getNum()) && !card2.getNum().equals(card3.getNum()))){
                    matchingNoOfProperty++;   
            }
            if ((card1.getcolor().equals(card2.getcolor()) && card1.getcolor().equals(card3.getcolor()) && card2.getcolor().equals(card3.getcolor())) || (!card1.getcolor().equals(card2.getcolor()) && !card1.getcolor().equals(card3.getcolor()) && !card2.getcolor().equals(card3.getcolor()))){
                matchingNoOfProperty++;   
            }
            if ((card1.getPattern().equals(card2.getPattern()) && card1.getPattern().equals(card3.getPattern()) && card2.getPattern().equals(card3.getPattern())) || (!card1.getPattern().equals(card2.getPattern()) && !card1.getPattern().equals(card3.getPattern()) && !card2.getPattern().equals(card3.getPattern()))){
                matchingNoOfProperty++;   
            }
            if ((card1.getShape().equals(card2.getShape()) && card1.getShape().equals(card3.getShape()) && card2.getShape().equals(card3.getShape())) || (!card1.getShape().equals(card2.getShape()) && !card1.getShape().equals(card3.getShape()) && !card2.getShape().equals(card3.getShape()))){
                matchingNoOfProperty++;   
            }

            //if all four property are same or different then set isset to true
            if (matchingNoOfProperty==4){
                isset=checkRepeat(checkcard1,checkcard2,checkcard3);
            }
            return isset;
        }   


        public boolean checkRepeat(String checkcard1, String checkcard2, String checkcard3){
            System.out.println("I'm inside check repeat");
            ArrayList<String> tempcards = new ArrayList<String>();

            boolean validSet = true;
            String card1 = checkcard1;
            String card2 = checkcard2;
            String card3 = checkcard3;
            if (foundCards.size()==0){
                System.out.println("I'm inside check repeat if");
                tempcards.add(card1);
                tempcards.add(card2);
                tempcards.add(card3);
                foundCards.add(tempcards);
                System.out.println("size of foundcards" + foundCards.size());
            }
            else {
                System.out.println("I'm inside check repeat else");
                for (int i=0 ; i<foundCards.size() ; i++){
                    int cardMatch =0 ;
                    for (int j=0; j <3 ; j++){
                        System.out.println("found card: " + foundCards.get(i).get(j));
                        System.out.println("cards to check1: " + card1);
                        System.out.println("cards to check2: " + card2);
                        System.out.println("cards to check3: " + card3);
                        if (foundCards.get(i).get(j).equals(card1) || foundCards.get(i).get(j).equals(card2) || foundCards.get(i).get(j).equals(card3)){
                            cardMatch++;
                        }
                    }
                    if (cardMatch ==3){
                        validSet =false;
                        break;
                    }
                }
            }
            return validSet;

        }
}
   public void displayCards(Cards[] cards) {
    //creating 3 empty cards
    Cards playCard1= new Cards("","","","");
    Cards playCard2= new Cards("","","","");
    Cards playCard3= new Cards("","","","");

    boolean isSet = false;
    boolean loop = true;
    Scanner scan = new Scanner(System.in);

         while (loop){
        System.out.println("\n");

        //displaying 12 cards on board
        board.toString(cards);

        System.out.println("\nTotal no of possible sets in this 12 set cards : " + possibleNumOfSet);
        System.out.println("Total no of sets you have found : " + setsFound);
        System.out.println("\nPlease enter a group of three cards as displayed");

        //first card
        System.out.print("Please enter first card : ");
        String card1 = scan.nextLine();
        if (card1.equalsIgnoreCase("quit")){    //stop the program
            System.out.println("\n THANK YOU for playing");
            System.exit(0);
        }
        String[] splitCard1 = validateCard(card1,cards);
        playCard1 = new Cards(splitCard1[0],splitCard1[1],splitCard1[2],splitCard1[3]);

        //second card
        System.out.print("Please enter second card : ");
        String card2 = scan.nextLine();
        if (card2.equalsIgnoreCase("quit")){    //stop the program
            System.out.println("\n THANK YOU for playing");
            System.exit(0);
        }
        String[] splitCard2 = validateCard(card2,cards);
        playCard2 = new Cards(splitCard2[0],splitCard2[1],splitCard2[2],splitCard2[3]);

        //third card
        System.out.print("Please enter third card : ");
        String card3 = scan.nextLine();
        if (card3.equalsIgnoreCase("quit")){    //stop the program
            System.out.println("\n THANK YOU for playing");
            System.exit(0);
        }
        String[] splitCard3 = validateCard(card3,cards);
        playCard3 = new Cards(splitCard3[0],splitCard3[1],splitCard3[2],splitCard3[3]);


        //calling checkSet function to check if chosen 3 cards is a set
        isSet = playCard1.CheckSet(playCard1, playCard2, playCard3);


        // if chosen 3 cards is a set then display congratulations message and increase value of setsFound by 1.
        if (isSet){
            System.out.print("\nCongratulations! You have found a set.\n\n");
            System.out.println("Your answer:");
            System.out.println("\t"+card1 + "    "+ card2 + "    "+ card3);
            System.out.println("Hit Enter to continue.");
            scan.nextLine();
            setsFound++;
        }
        else {
            System.out.println("\nWhoops.....  This is not a set");
            System.out.println("Hit Enter to continue.");
            scan.nextLine();
        }

        //if user finds all possible number of sets it will break the loop and display congratulations message.
        if (setsFound == possibleNumOfSet){
            gameFinish();
            break;
        }
    }