2013-03-22 19 views
0

我正在尝试做一些'邪恶的Hang子手游戏'(漂亮的斯坦福CS演习)。游戏的目的是通过尽可能多地移除尽可能多的字词解决方案来“欺骗”,以便用户在最终结束之前无法猜测。Java - 扫描器不会删除所有需要的对象

我已经做了一个循环(下图),似乎删除了许多单词可能的单词,但由于某种原因它并没有删除所有这些单词。输入是一个包含约120K字的dictionary.txt文件。

当我“猜”字母“a”时,它会带走大约60-70%的带有“a”的单词(基于输出与txt文件中第一对单词之间的比较进行估计)

File file = new File("dictionary.txt"); 
    Scanner textScan = new Scanner(file); 

    List<String> wordList = new ArrayList<String>(); 

    while (textScan.hasNext()) 
     { 
      word = textScan.next(); 
      wordList.add(word); 

     } 
    System.out.println("The ArrayList has " + wordList.size() + " objects stored in it."); 


    Scanner textScan1 = new Scanner(file); 

    for(int i = 0; i <= guessNumber; i++) 

    { 
     Collections.sort(wordList); 

     System.out.println("Type in your guess as a letter "); 
     String guess = keyboard.next(); 
     guess = guess.toLowerCase(); 

    while (textScan1.hasNext()) 
    { 
     String word1 = textScan1.next(); 
     if (wordLength != word1.length() && word1.contains(guess)) 
      { 
      wordList.remove(word1); 
      } 


    } 
    } 

我知道我的代码在这一点上有点混乱,我试图改进关于我的编程的一切,所以所有的反馈都非常感谢!我有一种感觉,那就是我不需要在那里等东西。

,我将发布的情况下,帮助下整个代码:

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




public class EvilHangman 

{ 


public static void main(String[] args) throws IOException 
{ 
    // declaring variables 
    int wordLength; 
    int guessNumber; 


    // initiate the scanner 
    Scanner keyboard = new Scanner(System.in); 




    // introduction and prompting the user for word length 

    System.out.println("Welcome to Hangman. Let's play! "); 
    System.out.println("Please enter the desired word length: "); 
    wordLength = keyboard.nextInt(); 

    while(wordLength < 0 || wordLength > 26) 
    { 
     System.out.println("This is not a valid word length. "); 
     System.out.println("Please enter the desired word length: "); 
     wordLength = keyboard.nextInt(); 
    } 

    // prompt the user for number of guesses 

    System.out.println("How many guesses do you want to have? "); 
    guessNumber = keyboard.nextInt(); 

    while(guessNumber < 0) 
    { 
     System.out.println("Number of guesses has to be a postive integer. "); 
     System.out.println("Please enter the desired number of guesses: "); 
     guessNumber = keyboard.nextInt(); 
    } 


    // count the number of words with the specified length 

    /* int wordCount = 0; 
    String word = null; 
    while (textScan.hasNext()) 
    { 
     word = textScan.next(); 
     if (word.length() == wordLength) 
      { 
      wordCount++; 
      } 


    } 
    */ 


    // prompts the user whether he/she wants a running count of word length - using next() instead of nextLine() to clear buffer 

    /* System.out.println("Do you want a running total of number of words remaining? "); 
    String runningTotal = keyboard.next(); 

    if (runningTotal.equalsIgnoreCase("yes")) 
     System.out.println("Words with that length: " + wordCount); 
    */ 

    // create a list (array) of all the words that matches the input length 
    String word = null; 


    File file = new File("dictionary.txt"); 
    Scanner textScan = new Scanner(file); 

    List<String> wordList = new ArrayList<String>(); 

    while (textScan.hasNext()) 
     { 
      word = textScan.next(); 
      wordList.add(word); 

     } 
    System.out.println("The ArrayList has " + wordList.size() + " objects stored in it."); 


    Scanner textScan1 = new Scanner(file); 

    for(int i = 0; i <= guessNumber; i++) 

    { 
     Collections.sort(wordList); 

     System.out.println("Type in your guess as a letter "); 
     String guess = keyboard.next(); 
     guess = guess.toLowerCase(); 

    while (textScan1.hasNext()) 
    { 
     String word1 = textScan1.next(); 
     if (wordLength != word1.length() && word1.contains(guess)) 
      { 
      wordList.remove(word1); 
      } 


    } 
    } 

    System.out.println("The ArrayList has " + wordList.size() + " objects stored in it."); 
    System.out.println(wordList); 
+2

字典中的所有单词都是小写字母吗?如果是这样,这行'guess = guess.toLowerCase();'也许是问题,因为你只是检查小写字母。 – 2013-03-22 02:21:40

回答

0

终于想通了它与扫描仪做。它必须在循环内启动

for(int i = 1; i <= guessNumber; i++) 

    { 
     Scanner textScan2 = new Scanner(file1);   
     System.out.println("Type in your guess as a letter "); 
     String guess = keyboard.next(); 
     //System.out.print(guess); 

    while (textScan2.hasNext()) 
    { 

     String word1 = textScan2.next(); 
     if (wordLength != word1.length() || (word1.contains(guess))) 
      { 
      wordList.remove(word1); 
      } 





    } 
    }