Java eAny()); printLine(); 打破 案例5: System.out.println(“计数(显示一张特定卡在牌组中的次数)”; Card newCard=createCard(); System.out.println(“此卡在此卡组中的次数为”+卡组计数(新卡)+“时间”); printLine(); 打破 案例6: System.out.println(“获取大小(牌组中的牌数)”; System.out.println(“此组中的卡数为“+Deck.getSize()+”卡”); printLine(); 打破 案例7: System.out.println(“打印组(打印组中的所有卡)”; System.out.println(deck.toString()); 打破 } } /* 如果用户选择插入卡选项,createCard方法将创建一张卡 @newCard-返回将在插入卡选项中使用的新卡 */ 公共静态卡createCard() { //用户需要的变量 内服; int值; System.out.println(“请创建一张卡。\n”); 系统输出打印(“输入套装:”; suit=键盘.nextInt(); System.out.print(“输入值:”); value=keyboard.nextInt(); //制作一张卡片 新卡=新卡(套装、价值); //还卡 归还新卡; } /* setDeck方法将初始化特定甲板的甲板手 @参数甲板-甲板手对象 */公共静态甲板(甲板手甲板) { //设置合适的值 对于(int i=1;i
,由于@Paul T.和@ATP,当我进行修改时,我的代码似乎正在工作。我将代码复制到另一个文件中,它开始工作。我真的不知道问题出在哪里 谢谢大家的帮助Java eAny()); printLine(); 打破 案例5: System.out.println(“计数(显示一张特定卡在牌组中的次数)”; Card newCard=createCard(); System.out.println(“此卡在此卡组中的次数为”+卡组计数(新卡)+“时间”); printLine(); 打破 案例6: System.out.println(“获取大小(牌组中的牌数)”; System.out.println(“此组中的卡数为“+Deck.getSize()+”卡”); printLine(); 打破 案例7: System.out.println(“打印组(打印组中的所有卡)”; System.out.println(deck.toString()); 打破 } } /* 如果用户选择插入卡选项,createCard方法将创建一张卡 @newCard-返回将在插入卡选项中使用的新卡 */ 公共静态卡createCard() { //用户需要的变量 内服; int值; System.out.println(“请创建一张卡。\n”); 系统输出打印(“输入套装:”; suit=键盘.nextInt(); System.out.print(“输入值:”); value=keyboard.nextInt(); //制作一张卡片 新卡=新卡(套装、价值); //还卡 归还新卡; } /* setDeck方法将初始化特定甲板的甲板手 @参数甲板-甲板手对象 */公共静态甲板(甲板手甲板) { //设置合适的值 对于(int i=1;i,java,arrays,class,while-loop,boolean,Java,Arrays,Class,While Loop,Boolean,,由于@Paul T.和@ATP,当我进行修改时,我的代码似乎正在工作。我将代码复制到另一个文件中,它开始工作。我真的不知道问题出在哪里 谢谢大家的帮助 -Rocky循环从不运行。将initialize更改为true,然后在找到时将其设置为false。除非您打算在循环中使用while(!found),但无论哪种方式,都要确保您不会进入无限循环的情况。(例如,如果找不到任何内容,如何使循环停止?)@PaulT.我这样做了,但我仍然得到了相同的输出-我的帖子上附加了一个图像。谢谢你的帮助!然后更新问
-Rocky循环从不运行。将initialize更改为true,然后在找到时将其设置为false。除非您打算在循环中使用
while(!found)
,但无论哪种方式,都要确保您不会进入无限循环的情况。(例如,如果找不到任何内容,如何使循环停止?)@PaulT.我这样做了,但我仍然得到了相同的输出-我的帖子上附加了一个图像。谢谢你的帮助!然后更新问题的代码,了解发生了什么变化。如果没有数据或类的其他部分,我们将无法运行你的示例。试着运行一个分步调试程序来检查变量,看看发生了什么,或者你是否已经完成了如果尚未使用,请在循环中添加一些System.out.println(…)
语句,以输出值,查看它们包含的内容,然后应该很明显情况可能是什么。代码似乎不完整?…错误显示:Card.getValue()未定义,DeckHand.count()未定义,和DeckHand.getSize()未定义
。
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;
}