gpt4 book ai didi

java - 一副牌 ArrayList

转载 作者:行者123 更新时间:2023-12-01 22:28:17 25 4
gpt4 key购买 nike

我的套牌类在 Suits 数组中迭代时似乎遇到了问题。

卡片类别:

//David Diienno
//Lab 01
import java.util.Arrays;
public class Card {

private String m_rank; // card rank: a number between 2 and 10, or Jack, Queen, King or Ace

private char m_suit; // card suit: S, C, H, or D (spades, clubs, hearts, or diamonds)
// Helpful supporting structures

private static String [] Ranks = {"2","3","4","5","6","7","8","9","10", "Jack", "Queen", "King", "Ace"};

private static char [] Suits = {'C','H','D','S'};
// Default constructor: sets card rank to 2 and card suit to H

public Card(){
//setting default card to a 2 of hearts
m_rank = Ranks[0];

m_suit = Suits[1];

}
// Accessors and Mutators
public Card(String Rank, char suit){
if(isValidRank(Rank) == true){
m_rank = Rank;

}
if(isValidSuit(suit) == true){
m_suit = suit;
}

}

public String getRank() {
System.out.println(m_rank);
return m_rank;
}

public char getSuit() {
System.out.println(this.suitToString());
return m_suit;
}

public void setRank(String rank) {
// Make sure to validate provided input
if (isValidRank(rank) == true){
m_rank = rank;
}
else{
System.out.println("The rank you entered is invalid");
}

}

public void setSuit(char suit) {
// Make sure to validate provided input
if(isValidSuit(suit) == true){
m_suit = suit;
}
else{
System.out.println("The suit you entered is invalid");
}

}
// Method toString – returns string representation of the card that looks as follows:
// 2 of Hearts, 3 of Spades, Jack of Diamonds, etc.
// Requirement: you must use switch statement to convert character to string.
//
// Hint: card’s rank is already a string. Therefore you only need to convert card suit into
// a string that reads “Clubs”, “Spades”, “Hearts” or “Diamond s”
public String suitToString(){
switch(m_suit){

case 'c': return "Clubs";

case 'h': return "Hearts";

case 'd': return "Diamonds";

case 's': return "Spades";
default: return "Hearts";

}
}

public String toString() {

String data = "You have a " + m_rank + " of " + this.suitToString();
return data;

}
// Supporting static methods
// Returns an array of possible card ranks

public static String [ ] getPossibleRanks() {

//System.out.println(Arrays.toString(Ranks));
for(int i = 0; i < Card.Ranks.length; i ++){
System.out.println(Ranks[i]);

}
return Ranks;
}

// Returns an array of possible card suits
public static char [ ] getPossibleSuits() {
for(int i = 0; i < Card.Suits.length;i++){

System.out.println(Suits[i]);

}
return Suits;
}

public boolean isValidRank (String r){
for(int i = 0; i < Card.Ranks.length; i++ ){
if(Card.Ranks[i].equals( r)){
return true;

}

}
return false;

}

public boolean isValidSuit (char s){
for (int i = 0; i < Card.Suits.length; i++){

if(Card.Suits[i] == s){

return true ;
}

}

return false;

}
}

套牌等级:

import java.util.*;
public class DeckOfCards extends Card {
private ArrayList <Card> deck;
private Card card;
private String [] Ranks = super.getPossibleRanks();
private char [] Suits = super.getPossibleSuits();

// public void resetDeckOfCards() {

public DeckOfCards()
deck = new ArrayList<Card>();
for(int s = 0; s < Suits.length; s++)
{
for(int r = 0; r < Ranks.length; r++){
card = new Card(Ranks[r],Suits[s]);
deck.add(card);
}

}

}
public void display(){
System.out.println(deck);

}
//public Card getCard() { /* ... */ }
//Remove a random card from the Array List of cards and return its value from this method Notes:
//1. Ensure that there is at least one card in the ArrayList of Cards
//2. If there are no more cards left in the ArrayList of Cards, reset the Deck of Cards
//3. Use class Random to create a random number which will be an index into ArrayList of Cards
//4. Remove and return card stored in the ArrayList of Cards in the randomly created index
// Return an ArrayList of specified size (i.e. returned cards are being removed from the deck). //
// Notes:
// 1. Use method getCard() to retrieve a single card
// 2. Validate the value of size passed into this method and return null if size is invalid
//public ArrayList<Card> getHand(int size) { /* ... */ }
}

为什么当我显示数组列表时,它不会遍历花色,而是遍历排名,我仍然得到 52 张牌,但都具有相同的花色。

最佳答案

所以问题似乎是您的 Suits 数组包含大写字母,但您的 suitToString 方法使用的是使用小写字母的 switch 语句。由于默认情况是红心,我打赌你所有的牌都是红心。

关于java - 一副牌 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com