2011-12-20 4 views
0

이 프로그램에서는 Letters ('Letters'는 클래스 임)을 추가하여 LinkedList('lettersLeft')을 만들었습니다. 각 문자에는 3 개의 값이 있습니다 : 1) 해당 문자 (즉 'A' 게임에서 나타나는 시간 (즉, '9'), 3) 얻은 점수 (즉 '1'). 또한 getNextLetter()으로 가방에서 다음 임의의 문자를 얻습니다. 나는 그 값 (예 : "A, 9,1 D, 4,2")을 가진 가방에서 2 개의 무작위 문자를 인쇄 할 for 루프를 만들고 싶습니다. 이것은 (당신이 이해 해달라고하면 뭔가 물어) 내 코드입니다임의의 문자를 인쇄합니다 - LinkedList

Letters_bag :

public class Letters_bag { 

public static final Letters A = new Letters('a', 9, 1); 
public static final Letters B = new Letters('b', 2, 3); 
public static final Letters C = new Letters('c', 2, 3); 
public static final Letters D = new Letters('d', 4, 2); 
public static final Letters E = new Letters('e', 12, 1); 
public static final Letters F = new Letters('f', 2, 4); 


public static final Letters[] allLetters = new Letters[] { 
    Letters_bag.A, 
    Letters_bag.B, 
    Letters_bag.C, 
    Letters_bag.D, 
    Letters_bag.E, 
    Letters_bag.F, 

}; 


LinkedList<Letters> lettersLeft = new LinkedList(); 


public Letters_bag() { 
    // add all the letters 
    addLetter(A); 
    addLetter(B); 
    addLetter(C); 
    addLetter(D); 
    addLetter(E); 
    addLetter(F); 

} 

// helper method to add the letters 
private void addLetter(Letters sl) { 
    for (int i=0;i<sl.getCount();i++) { 
     this.lettersLeft.add(sl); 
    } 
} 
/** 
*Returns the next random letter from the bag. 
*/ 
Letters getNextLetter() { 
    // shuffle those letters 
    Collections.shuffle(lettersLeft); 
    // return a random letter 
    return lettersLeft.removeFirst(); 
} 

} 

편지 :

public class Letters { 
private char value; 
private int count; 
private int points; 

public Letters(char value, int count, int points) { 
    this.value = value; 
    this.count = count; 
    this.points = points; 
} 

public char getValue() { 
    return value; 
} 

public int getCount() { 
    return count; 
} 


public int getPoints() { 
    return points; 
} 

답변

1

당신은 같은 의미합니까?

Collections.shuffle(allLetters); 
for(int i=0;i<letterCount;i++) 
    System.out.println(allLetters[i]); 

IDE는 단순화 할 수있는 toString 메서드를 생성 할 수 있습니다. (또는 자신을 써주세요)

+0

답장을 보내 주셔서 감사합니다. 나는 그것에 대해 노력하고 있습니다. 왜이 "[email protected]"과 같은 것을 다시 돌려 보내고 있습니까? – nick

+0

Collections.shuffle은 linkedList의 문자를 randomly.right로 인쇄하기를 원하기 때문에 Collections.shuffle (lettersLeft)이어야한다고 생각합니다. (Collections.shuffle (allLetters) beacause가 나에게 오류 메시지를 표시합니다) – nick

+1

" [email protected] "Letters 클래스에는 .toString 메서드가 필요하기 때문입니다. 이것이 없으면 Object에서 기본 toString 메서드를 얻고 클래스 이름과 메모리 주소를 출력합니다. –

관련 문제