2014-05-11 39 views
1

在我目前的任務中,我有一個文本文件,其中包含大量存儲在列表中的單詞。我的一個方法必須將此列表中的某個(用戶指定的)長度的所有單詞存儲到一個集合中。此外,不同的列表被分成兩個不同的類別。我的問題是:如何檢索該列表中的單個元素的長度?編輯:這裏是完整的類。如何檢索一組中單個字符串的長度?

文件和目錄都設置這樣的客戶端類,HangmanMain:

//類HangmanMain對於劊子手程序驅動程序。它讀取一個
//在遊戲中使用的單詞詞典,然後用 //用戶進行遊戲。這是一個嘮叨的hang子手,延遲選擇一個詞 //以保持其選項開放。您可以更改SHOW_COUNT的設置,以查看 //每回合仍有多少選項。

import java.util.*; 
import java.io.*; 

public class HangmanMain { 
public static final String DICTIONARY_FILE = "E:/CSC143/Workspace/Assignment2/src/dictionary.txt"; 
public static final boolean DEBUG = false; // show words left 

public static void main(String[] args) throws FileNotFoundException { 
    System.out.println("Welcome to the cse143 hangman game."); 
    System.out.println(); 

    // open the dictionary file and read dictionary into an ArrayList 
    Scanner input = new Scanner(new File(DICTIONARY_FILE)); 
    List<String> dictionary = new ArrayList<String>(); 
    while (input.hasNext()) { 
     dictionary.add(input.next().toLowerCase()); 
    } 

    // set basic parameters 
    Scanner console = new Scanner(System.in); 
    System.out.print("What length word do you want to use? "); 
    int length = console.nextInt(); 
    System.out.print("How many wrong answers allowed? "); 
    int max = console.nextInt(); 
    System.out.println(); 

    // set up the HangmanManager and start the game 
    List<String> dictionary2 = Collections.unmodifiableList(dictionary); 
    HangmanManager hangman = new HangmanManager(dictionary2, length, max); 
    if (hangman.words().isEmpty()) { 
     System.out.println("No words of that length in the dictionary."); 
    } else { 
     playGame(console, hangman); 
     showResults(hangman); 
    } 
} 

// Plays one game with the user 
public static void playGame(Scanner console, HangmanManager hangman) { 
    while (hangman.guessesLeft() > 0 && hangman.pattern().contains("-")) { 
     System.out.println("guesses : " + hangman.guessesLeft()); 
     if (DEBUG) { 
      System.out.println(hangman.words().size() + " words left: "+ hangman.words()); 
     } 
     System.out.println("guessed : " + hangman.guesses()); 
     System.out.println("current : " + hangman.pattern()); 
     System.out.print("Your guess? "); 
     char ch = console.next().toLowerCase().charAt(0); 
     if (hangman.guesses().contains(ch)) { 
      System.out.println("You already guessed that"); 
     } else { 
      int count = hangman.record(ch); 
      if (count == 0) { 
       System.out.println("Sorry, there are no " + ch + "'s"); 
      } else if (count == 1) { 
       System.out.println("Yes, there is one " + ch); 
      } else { 
       System.out.println("Yes, there are " + count + " " + ch+ "'s"); 
      } 
     } 
     System.out.println(); 
    } 
} 

// reports the results of the game, including showing the answer 
public static void showResults(HangmanManager hangman) { 
    // if the game is over, the answer is the first word in the list 
    // of words, so we use an iterator to get it 
    String answer = hangman.words().iterator().next(); 
    System.out.println("answer = " + answer); 
    if (hangman.guessesLeft() > 0) { 
     System.out.println("You beat me"); 
    } else { 
     System.out.println("Sorry, you lose"); 
    } 
} 
} 

我的方法是在HangmanManager類中設置的。

import java.util.*; 
import java.io.*; 

public class HangmanManager { 
private List<String> dictionary; 
private int length; 
private int max;  
private Set<String> w = new HashSet<String>(); 
private SortedSet<Character> guess; 

Integer L1 = new Integer(length); 
public HangmanManager (List<String> dictionary, int length, int max){ 
    this.dictionary = dictionary; 
    this.length = length; 
    this.max = max; 
} 
public Set<String> words(){ 

    while (scan.hasNext()){ 
     if (scan.next().equals(L1)){ 
      w.add(scan.next()); 
     } 
    } 
    return w; 

} 

public int guessesLeft(){ 

    return max; 

} 

public SortedSet <Character> guesses(){ 
    Scanner input = new Scanner (System.in); 

    return guess; 

} 

public String pattern(){ 
    return null; 

} 

public int record (char guess){ 
    return guess; 

} 



} 

Scan只是一個佔位符名稱。正如你可以在第一個塊中看到的那樣,while循環在「dictionary」列表中添加DICTIONARY_FILE中的所有字符串。我想在第二個模塊中做的事情是將所有長度合適的單詞添加到w列表中,但是我不知道如何從完全不同的類中讀取文件(如果甚至可能的話),而且我也不會知道如何獲取所述文件中每個單獨元素的長度。你們有什麼想法嗎?或者你需要我上傳更多信息。 PS:我的任務完整標題是「邪惡的Hang子手」。從我看起來,它是一個相當常見的編程任務,所以你應該能夠谷歌它,並獲得更多的信息,我的意思。

PSS:不介意HangmanManager中的其他方法。我主要關注「Words」方法。

感謝您的幫助。

+0

請不要忘記標記您正在使用的編程語言。我刪除了eclipse標籤並添加了java;因爲這個問題不適合eclipse標籤。 –

+0

抱歉。 – Sol420

回答

0

好的,假設你有所有設置的單詞列表,這裏有一些僞代碼來指導你轉發。

for every string in set 
    check if word is of specified length 
     if true: 
     add word to set 
     if false: 
     do nothing 

String api可能有一些這方面的有用的方法,尤其是用於確定字符串的長度.. :)

關於你問題的後半部分,它是有點難以回答如何進入事在另一個沒有更多代碼的類中看看它是如何設置的。通常,您使用getter方法來檢索該字段。

+0

當你說「設置列表」時,你的意思是把它放在客戶端類還是支持類? – Sol420

+0

你可以從任何一個訪問它,但沒有看到更完整的代碼,它不可能幫助那個部分 – Zavior

+0

我希望避免做一個像我以前的問題一樣的巨大的帖子,但我認爲太久會比太短。我會在一分鐘後發佈完整的課程。 – Sol420

相關問題