首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java crossword :有没有办法计算打印单词前后的空格?

Java crossword :有没有办法计算打印单词前后的空格?
EN

Stack Overflow用户
提问于 2019-02-17 20:07:12
回答 1查看 85关注 0票数 0

Words:测试文件

我正在为我的学校建立一个项目,但我遇到了麻烦。你能帮我找到一种打印单词前后空格的方法吗?

代码语言:javascript
复制
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

    }

}
EN

回答 1

Stack Overflow用户

发布于 2019-02-17 21:01:19

你已经有了关键的成分:indexOf()

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

我们必须记住单词,因为我们要经历两次整个循环。

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

它只是一个前进的垫脚石。还有很多事情要做,例如,你可以决定只使用小写的单词,然后在最终输出中将“热点”字符设为大写。

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

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

代码语言:javascript
复制
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);
}

输出示例:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54733076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档