【发布时间】:2021-03-23 21:34:12
【问题描述】:
万劫不复,
更新:我已添加其余代码。我不想以 StackOverFlow 阻止我发布的方式添加太多代码。感谢您的帮助!
在我的作业中,它声明“删除 - 从 DeckHand 中删除具有给定值的卡片的一个实例并返回已删除的实例。”
一张牌等于花色。
我初始化了一整套 DeckHand 类,其中包含:红桃 A,.....,黑桃 A,.......,方块 A,.....,梅花 A .
我被告知该参数是一个值(表示为整数),并且一旦找到该值的第一个实例以创建卡片并从 DeckHand 中删除,就在 delete 方法中。
例如,我试图删除红桃五,但我的程序不断删除梅花五。 我已经提供了我的代码和几张图片。
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;
}
【问题讨论】:
-
循环永远不会运行。将初始化更改为true,然后在找到时设置为false。除非您打算在循环中使用
while(!found),但无论哪种方式,请确保您不会陷入无限循环的情况。 (例如,如果什么也没找到,什么会使循环停止?) -
@PaulT。我这样做了,但我仍然得到相同的输出 - 我在帖子上附加了一张图片。感谢您的帮助!
-
然后更新问题的代码以了解更改的内容。如果没有数据或课程的其余部分,我们将无法运行您的示例。尝试运行逐步调试器来检查变量以查看发生了什么,或者如果您还没有使用过,请在循环内添加一些
System.out.println(...)语句以输出值以查看它们包含的内容,然后情况应该很明显。 -
代码似乎不完整? ...错误显示:
Card.getValue() is undefined、DeckHand.count() is undefined和DeckHand.getSize() is undefined。
标签: java arrays class while-loop boolean