Java填字游戏:有没有办法计算打印单词前后的空格?

问题描述 投票:0回答:1

Words:测试文件

我正在为我的学校设立一个课程,但我遇到了麻烦。你可以帮我找到一种方法来打印前面和后面的空格吗?

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

    }

}
java puzzle crossword
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
© www.soinside.com 2019 - 2024. All rights reserved.