0

我正在尝试制作某种“邪恶的刽子手游戏”(漂亮的斯坦福 CS 练习)。游戏的目的是通过删除尽可能多的单词解决方案来“作弊”,以便用户在最后之前无法猜测。

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

当我“猜测”字母“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);
4

1 回答 1

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);
            }





    }
    }
于 2013-03-22T03:37:30.097 回答