从甲板工中删除卡片

u0njafvf  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(246)

见鬼去吧,
更新:我已经添加了剩下的代码。我不想添加太多的代码,这样stackoverflow会阻止我发布。谢谢你的帮助!
在我的作业中,它声明“删除-从甲板工处删除具有给定值的卡的一个示例,并返回已删除的示例。”
一张卡相当于一套衣服的价值。
我初始化了一套完整的甲板手类,包括:红桃王牌,…,黑桃王牌,…,钻石王牌,…,梅花王牌。
我被告知该参数是一个值(表示为整数),在delete方法中,一旦找到该值的第一个示例,就创建一张卡片并从deckhand中删除。
举个例子,我试图删除五颗红心,但我的程序一直在删除五颗红心。我已经提供了我的代码和一些图片。
我会很感激你的帮助。谢谢您!

//主程序

public class Test
{
   private static final int SUIT = 4;
   private static final int VALUE = 13;
   public static Scanner keyboard = new Scanner(System.in);

   public static void main(String[] args)
   {
      //Scanner keyboard
      // Scanner keyboard = new Scanner(System.in);

      //Variables needed from User
      char decision = 'Y';
      int optionSelected;
      int chooseDeck;

      //Display Opening Messasge
      System.out.println("Welcome to Phase I DeckHand Implementation\n");
      System.out.println("Please note that DeckOne is fully intialized and DeckTwo is empty.");

      //Declare two instances of the DeckHand
      DeckHand deckOne = new DeckHand();
      DeckHand deckTwo = new DeckHand();

      //Initialize one Deck - deckOne
      setDeck(deckOne);

      while(!(decision == 'N' || decision == 'n'))
      {
         //Display Menu
         displayMenu();

         //Select Option from Menu
         optionSelected = chooseMenuOption();

         //Select Deck to Manipulate
         chooseDeck = chooseDeckHand();

         //Perform Option
         if(chooseDeck == 1)
         {
            performOption(optionSelected, deckOne);
         }
         else if (chooseDeck == 2)
         {
            performOption(optionSelected, deckTwo);
         }

         System.out.println("Would you like to continue with this Program?");
         System.out.print("Select y/Y for Yes or Select n/N for No: " );
         decision = keyboard.next().charAt(0);
         printLine();
         if(decision == 'n' || decision == 'N')
         {
            System.out.println("Thank you for using this Program");
         }    
      }     
   }

/*
The displayMenu Method will display the menu to the user

* /

   public static void displayMenu()
   {
      System.out.println("Please select from the options below: ");
      System.out.println("Option 1: Initialize An Empty DeckHand");
      System.out.println("Option 2: Insert a Card");
      System.out.println("Option 3: Delete a Card");
      System.out.println("Option 4: Delete Any Random Card");
      System.out.println("Option 5: Count(Shows how many times a specific Card is in the Deck)");
      System.out.println("Option 6: Get Size (Number of Cards in a Deck)");
      System.out.println("Option 7: Print Deck (Prints all Cards in a Deck)\n");
   }
/*
The chooseMenuOption Method will ask the user to choose an option from the Menu.
@return option - Will return either 1-7 in order to perfom the operation from the Menu.

* /

   public static int chooseMenuOption()
   {      
      //Variable for User
      int option;

      //Message
      System.out.print("Please select an option from the Menu: ");
      option = keyboard.nextInt();
      printLine();
      return option;  
   }
/*
The chooseDeckHand Method will ask the user choose which Deck to manipulate
either DeckOne or DeckTwo
@return choose - If user selects one they will choose DeckOne. If user selects two they will choose DeckTwo

* /

   public static int chooseDeckHand()
   {
      DeckHand deckChosen = new DeckHand();
      int choose;

      System.out.println("Please select which Deck you would like to manipulate.");
      System.out.println("Select (1) for DeckOne or Select (2) for DeckTwo.");
      System.out.print("Please select Deck: ");
      choose = keyboard.nextInt();
      printLine();
      return choose;
   }

/*
The performOption Method will perform the option selected from the menu to the DeckHand selected.
@param option - The option chosen from the user based on the menu.
@param deck - The DeckHand the user chose to use

* /

   public static void performOption(int option, DeckHand deck)
   {            
      //Switch Statement
      switch(option)
      {
         case 1:
            deck = new DeckHand();
            System.out.println("Initialized an Empty DeckHand");
            break;

         case 2:
            System.out.println("Insert a Card.");
            Card addCard = createCard();
            deck.insert(addCard);
            System.out.println("A Card has been added to this Deck.");
            printLine();
            break;

         case 3:
            System.out.println("Delete a Card.");
            int value;
            System.out.print("Please enter a value to Delete: ");
            value = keyboard.nextInt();

            //Check to see if the Card they entered exists in the list
            if(deck.count(value) == 0)
            {
               System.out.println("Unfortunately, this card does not exist.");
            }
            else
            {
               System.out.println("Deleted " + deck.delete(value));
               printLine();
            }
            break;

         case 4:
            System.out.println("Delete Any Random Card.");
            System.out.println("Deleted " + deck.deleteAny());
            printLine();
            break;

         case 5:
            System.out.println("Count(Shows how many times a specific Card is in the Deck)");
            Card newCard = createCard();
            System.out.println("The number of times this card is in this deck is " + deck.count(newCard) + "time(s)");
            printLine();
            break;

         case 6:
            System.out.println("Get Size (Number of Cards in a Deck)");
            System.out.println("The number of cards in this Deck is " + deck.getSize() + "card(s).");
            printLine();
            break;

         case 7:
            System.out.println("Print Deck (Prints all Cards in a Deck)");
            System.out.println(deck.toString());
            break;          
      }
   }

/*
The createCard Method will create a Card if the user selects the Insert Card option
@newCard - returns a new Card that will be used in the Insert Card option

* /

   public static Card createCard()
   {  
      //Variables needed from User
      int suit;
      int value;

      System.out.println("Please create a Card.\n");
      System.out.print("Enter a suit: ");
      suit = keyboard.nextInt();
      System.out.print("Enter a value: ");
      value = keyboard.nextInt();

      //Create a Card 
      Card newCard = new Card(suit, value);

      //Return the Card
      return newCard;  
   }

/*
The setDeck Method will initialize the DeckHand for a particular deck
@param deck - DeckHand object

* /    public static void setDeck(DeckHand deck)

   {
      //Set _suit values
      for(int i = 1; i <= SUIT; i++)
      {
         for(int j = 1; j <= VALUE; j++)
         {
            Card newCard = new Card(i,j);
            deck.insert(newCard);
         }
      }
   }

/*
The printLine Method will perform the System.out.println()

* /    public static void printLine()

   {
      System.out.println();
   }
}

class DeckHand
{
   //Data Members
   private static final int FIXED = 52;
   private Card[] _list = new Card[FIXED]; // Creates an Array of 52 Cards
   private int _size; //Size of the Card _list
   private static Random randomNumbers = new Random(); //Random Object

   //Constructor
   public DeckHand()
   {
      _list = new Card[FIXED];
      _size = 0; 
   }

   //insert Method: insert a given Card in the DeckHand.
   public void insert(Card add)
   {
      //Note: This code was modified from CSC205 - Sample Stack Class - push() method
      if(_size >= _list.length)
      {
         Card[] temp = new Card[_list.length + FIXED];
         for(int i = 0; i < _size; i++)
         {
            temp[i] = _list[i];
         }
         _list = temp;
      }
      _list[_size] = add;
      _size++;   
   }

/*
The delete Method will delete one instance of a Card with a given value from the DeckHand
and return the deleted instance.
@param givenValue: An integer representing a Value of a Card (E.X. - Ace, 2, 3, Jack, Queen King)
@return delteCard: The instance of the deleted Card

* /

   public Card delete(int givenValue)
   {
      int valueFound = 0;
      int suitFound = 0;
      int cardFound = 0;
      boolean notFound = true;  
      int i = 0;
      while(notFound)
      {
         if(givenValue == _list[i].getValue())
         {
            //Save i value to cardFound
            cardFound = i;
            valueFound = _list[i].getValue();
            suitFound = _list[i].getSuit();

            //Change Found to true so it exits the While Loop
            notFound = false;
         }
         i++;
      }
      //Create a New Card that will be deleted
      Card deletedCard = new Card(suitFound, valueFound);

      //Now replace Card index with the lastCard and subtract one from size
      _list[cardFound] = _list[_size - 1];
      _size--;

      //Return DeletedCard
      return deletedCard;
   }

/*
The deleteAny Method will delete one instance of a Card selected at random from the DeckHand
and return the deleted instance.
@return delteCard: The instance of the deleted Card

* /

   public Card deleteAny()
   {
      //Randomly select a card from the DeckHand
      int randomCard = randomNumbers.nextInt(_size);
      Card deletedCard = _list[randomCard];

      //Replace Card index with the lastCard and subtract one from the size
      _list[randomCard] = _list[_size - 1];
      _size--;

      //Return the deleted instance of the Card
      return deletedCard;
   }
92dk7w1h

92dk7w1h1#

在@pault的帮助下,当我进行修改时,我的代码似乎正在运行。和@atp。我把我的代码复制到另一个文件,它就开始工作了。我真的不知道是什么问题。
谢谢大家的帮助!
-洛基

相关问题