0

:测试文件

我正在为我的学校设置一个程序,但我遇到了麻烦。你能帮我找到一种方法来打印打印的单词前后的空格吗?

public class Main {

    public static void main(String[] args) {
        System.out.println("crossword generator ver. 1.0");
        File wordlist = new File("words.txt");
        try {
            Scanner s = new Scanner(wordlist);
            String words[] = new String[1000000];
            int lineNr = 0;
            while (s.hasNext() && lineNr < 1000000) {
                words[lineNr] = s.nextLine();
                lineNr++;
            }
            System.out.println("Wordlist succesfully loaded");
            Random r = new Random();
            String solution = words[r.nextInt(lineNr)];
            System.out.println("Solution = " + solution);
            for (int i = 0; i<solution.length(); i++){
                char c = solution.charAt(i);
                String word;
                do{
                    word = words[r.nextInt(lineNr)];                    
                } while(word.indexOf(c) == -1);
                System.out.printf("(%c): %s \n", c ,word);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // TODO Auto-generated method stub

    }

}
4

1 回答 1

1

您已经拥有其中的关键成分:indexOf()

创建空格的数量有点棘手:创建一个与 indexOf 相同的数量与我们需要的完全相反。首先,我们必须计算最高的 indexOf,这样我们就可以在每个单词前面减去当前单词中的 indexOf 后创建该数量的空格。

而且我们必须记住这些单词,因为我们经历了整个循环两次。

下面的解决方案有点脏 - 更好的方法是为随机单词的实例创建一个新类(带有它们的小写版本和 indexOf),这也可以保存一个有效 indexOf 位置的列表,这样你就不会总是使用字符的第一次出现。

它只是为了成为路上的垫脚石。还有很多工作要做,例如,您可以决定只使用小写单词,然后在最终输出中将“热门”字符设为大写。

此代码忽略大写/小写,因此如果您的解决方案单词以大写字符开头,您不会被锁定在某些随机单词中。这里的实现方式也很脏。

顺便说一句,加载列表可以大大简化,如下所示。这也将避免不必要的大单词列表数组(否则有时可能太小)。

public static void main(String[] args) {

    System.out.println("\ncrossword generator ver. 1.0");

    // Load word list.
    final List<String> wordList;
    try {
        final File wordListFile = new File("words.txt");
        wordList = Files.readAllLines(wordListFile.toPath(), StandardCharsets.UTF_8);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    System.out.println("\nWord list successfully loaded.");


    // Pick solution word.
    final int wordCount = wordList.size();
    final Random rand = new Random();
    final String solution = wordList.get(rand.nextInt(wordCount));
    final String solutionLC = solution.toLowerCase(); // So that we won't depend on upper/lower case.
    final int solutionLen = solution.length();
    System.out.println("\nSolution = " + solution + "\n");


    // Choose words whose characters are in the solution word.
    final String[] chosenWords = new String[solutionLen];
    int highestIndex = 0;
    for (int i = 0; i < solutionLen; i++) {
        final char c = solutionLC.charAt(i);
        String word;
        int indexOfChar;
        do {
            word = wordList.get(rand.nextInt(wordCount));
            indexOfChar = word.toLowerCase().indexOf(c);
        } while (indexOfChar < 0);
        chosenWords[i] = word;
        highestIndex = Math.max(highestIndex, indexOfChar);
    }


    // Print crossword excerpt.
    for (int i = 0; i < solutionLen; i++) {
        final char cLC = solutionLC.charAt(i);
        final char c = solution.charAt(i);
        final int indexOfChar = chosenWords[i].toLowerCase().indexOf(cLC);
        System.out.println("(" + c + "): " + createStringOfIdenticalCharacters(highestIndex - indexOfChar,
                                                                               ' ') + chosenWords[i]);
    }

}


public static String createStringOfIdenticalCharacters(final int count,
                                                       final char c) {
    final char[] retPreliminary = new char[count];
    Arrays.fill(retPreliminary, c);
    return new String(retPreliminary);
}

示例输出:

crossword generator ver. 1.0

Word list successfully loaded.

Solution = councilor

(c):            Corcyra
(o):        Harbour
(u):      nonillustrative
(n):           unexiled
(c):       sepulchering
(i):        Torrington
(l):         builtin
(o):           nonnarcissistic
(r): Balsamodendron
于 2019-02-17T13:01:19.050 回答