2017-10-25 9 views
-1

30 분 동안 검색 한 결과 단어와 값에 관한 답변이 배열에 없습니다.배열에 단어와 정수를 모두 삽입합니다.

그래서 카드 갑판을 만들려고하는데 배열의 슬롯 값을 "클럽"으로 지정하여 슬롯 [4]의 값을 "5 클럽".

가능합니까? 배열은 String으로 정의됩니다.

감사합니다.

이것은 내 줄입니다. (틀렸어요.) deck [j] [k] = "(j + 1) 개 클럽";

+0

시도 = (J + 1) + "클럽" – Jens

+3

당신은 어떤 자바 초보자 리소스를 잡아 시작할 수 있습니다 (튜토리얼, 비디오, (/ 모든 코드를 알고 O를 승)입니다 가장 좋은 SOLN 내가 올 수 책, ...) 당신은 몇 가지 기본적인 것들로 고투. 물론'String [] 데크를 만들고 루프를 사용하여''deck [i] = i + ''로 초기화 할 수 있습니다. 그러나 ** OOP ** 방식으로 전용 'Card' 클래스를 만든 다음 Card [] deck 같은 것을 사용할 수도 있습니다. 따옴표를 사용할 때'String'을 시작한다는 것을 나타냅니다. 거기에 값을 넣고 싶으면'i + "..."를 사용하여 텍스트를 끝내고'int'의'String' 값을 추가해야합니다. – Zabuza

+0

작업 중! 감사 :) –

답변

0

동질 컬렉션이 필요합니다. 즉, 다른 유형을 보유 할 수있는 컬렉션을 의미합니다. 이것은 자바에서 가능하지만, 나는 당신의 필요에 대해 강력히 추천한다.

대신 카드와 함께 2 가지 속성 (정장과 값)을 만들 수 있습니다. 그런 다음 간단하게 카드 컬렉션을 만들 수 있습니다 (동종 컬렉션 - 하나의 유형 만 보유 할 수있는 컬렉션). 같은

뭔가 :

public class Card { 

    private String _suit; 
    private Int _number 

    public Card(String suit, Int number) { 
     _suit = suit 
     _number = number 
    } 

    public String getSuit() { 
     return _suit 
    } 
    public void setSuit(suit: String) { 
     _suit = suit 
    } 

    public Int getNumber() { 
     return _number 
    } 
    public void setNumber(number: Int) { 
     _number = number 
    } 
} 

그리고 다음과 같이 카드의 컬렉션을 가질 수 있습니다

private Collection<Card> deck = ArrayList<Card>() 

deck.add(new Card("Diamonds", 5)) 
1

나는이 접근법에 일반적으로 접근 할 수 있다고 생각합니다. 카드 갑판을 만드는 것은 모든 객체 지향 언어에서 일반적으로 사용되는 시나리오입니다. 랭크 필드와 수트 필드가 모두있는 사용자 정의 유형을 정의한 다음 해당 유형의 배열을 만드는 것이 좋습니다. Deck of cards JAVA

은 자바 카드의 갑판에 대한 Google 검색을 수행하십시오 :

이 질문을 참조하십시오. 다음은 멋진 예제입니다. tutorial :

/** 
* An object of type Card represents a playing card from a 
* standard Poker deck, including Jokers. The card has a suit, which 
* can be spades, hearts, diamonds, clubs, or joker. A spade, heart, 
* diamond, or club has one of the 13 values: ace, 2, 3, 4, 5, 6, 7, 
* 8, 9, 10, jack, queen, or king. Note that "ace" is considered to be 
* the smallest value. A joker can also have an associated value; 
* this value can be anything and can be used to keep track of several 
* different jokers. 
*/ 
public class Card { 

    public final static int SPADES = 0; // Codes for the 4 suits, plus Joker. 
    public final static int HEARTS = 1; 
    public final static int DIAMONDS = 2; 
    public final static int CLUBS = 3; 
    public final static int JOKER = 4; 

    public final static int ACE = 1;  // Codes for the non-numeric cards. 
    public final static int JACK = 11; // Cards 2 through 10 have their 
    public final static int QUEEN = 12; // numerical values for their codes. 
    public final static int KING = 13; 

    /** 
    * This card's suit, one of the constants SPADES, HEARTS, DIAMONDS, 
    * CLUBS, or JOKER. The suit cannot be changed after the card is 
    * constructed. 
    */ 
    private final int suit; 

    /** 
    * The card's value. For a normal card, this is one of the values 
    * 1 through 13, with 1 representing ACE. For a JOKER, the value 
    * can be anything. The value cannot be changed after the card 
    * is constructed. 
    */ 
    private final int value; 

    /** 
    * Creates a Joker, with 1 as the associated value. (Note that 
    * "new Card()" is equivalent to "new Card(1,Card.JOKER)".) 
    */ 
    public Card() { 
     suit = JOKER; 
     value = 1; 
    } 

    /** 
    * Creates a card with a specified suit and value. 
    * @param theValue the value of the new card. For a regular card (non-joker), 
    * the value must be in the range 1 through 13, with 1 representing an Ace. 
    * You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING. 
    * For a Joker, the value can be anything. 
    * @param theSuit the suit of the new card. This must be one of the values 
    * Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER. 
    * @throws IllegalArgumentException if the parameter values are not in the 
    * permissible ranges 
    */ 
    public Card(int theValue, int theSuit) { 
     if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && 
      theSuit != CLUBS && theSuit != JOKER) 
     throw new IllegalArgumentException("Illegal playing card suit"); 
     if (theSuit != JOKER && (theValue < 1 || theValue > 13)) 
     throw new IllegalArgumentException("Illegal playing card value"); 
     value = theValue; 
     suit = theSuit; 
    } 

    /** 
    * Returns the suit of this card. 
    * @returns the suit, which is one of the constants Card.SPADES, 
    * Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER 
    */ 
    public int getSuit() { 
     return suit; 
    } 

    /** 
    * Returns the value of this card. 
    * @return the value, which is one of the numbers 1 through 13, inclusive for 
    * a regular card, and which can be any value for a Joker. 
    */ 
    public int getValue() { 
     return value; 
    } 

    /** 
    * Returns a String representation of the card's suit. 
    * @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs" 
    * or "Joker". 
    */ 
    public String getSuitAsString() { 
     switch (suit) { 
     case SPADES: return "Spades"; 
     case HEARTS: return "Hearts"; 
     case DIAMONDS: return "Diamonds"; 
     case CLUBS: return "Clubs"; 
     default:  return "Joker"; 
     } 
    } 

    /** 
    * Returns a String representation of the card's value. 
    * @return for a regular card, one of the strings "Ace", "2", 
    * "3", ..., "10", "Jack", "Queen", or "King". For a Joker, the 
    * string is always numerical. 
    */ 
    public String getValueAsString() { 
     if (suit == JOKER) 
     return "" + value; 
     else { 
     switch (value) { 
     case 1: return "Ace"; 
     case 2: return "2"; 
     case 3: return "3"; 
     case 4: return "4"; 
     case 5: return "5"; 
     case 6: return "6"; 
     case 7: return "7"; 
     case 8: return "8"; 
     case 9: return "9"; 
     case 10: return "10"; 
     case 11: return "Jack"; 
     case 12: return "Queen"; 
     default: return "King"; 
     } 
     } 
    } 

    /** 
    * Returns a string representation of this card, including both 
    * its suit and its value (except that for a Joker with value 1, 
    * the return value is just "Joker"). Sample return values 
    * are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades", 
    * "Joker", "Joker #2" 
    */ 
    public String toString() { 
     if (suit == JOKER) { 
     if (value == 1) 
      return "Joker"; 
     else 
      return "Joker #" + value; 
     } 
     else 
     return getValueAsString() + " of " + getSuitAsString(); 
    } 


} // end class Card 
0

난 당신의 코드를 검토하여 w/o 특정 SOLN에게 캔트.

String card = (j+1)+" of clubs"; 
String deck[]=new String[]{card};