Java纵横字谜:有没有办法计算打印的单词前后的空格?
:测试文件 我正在为我的学校设置一个程序,但我遇到了麻烦。你能帮我找到一种打印单词前后空格的方法吗Java纵横字谜:有没有办法计算打印的单词前后的空格?,java,crossword,Java,Crossword,:测试文件 我正在为我的学校设置一个程序,但我遇到了麻烦。你能帮我找到一种打印单词前后空格的方法吗 public class Main { public static void main(String[] args) { System.out.println("crossword generator ver. 1.0"); File wordlist = new File("words.txt"); try { S
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
}
}
公共类主{
公共静态void main(字符串[]args){
System.out.println(“纵横字谜生成器1.0版”);
File wordlist=新文件(“words.txt”);
试一试{
扫描器s=新扫描器(单词列表);
字符串字[]=新字符串[1000000];
int-lineNr=0;
而(s.hasNext()&&lineNr<1000000){
单词[lineNr]=s.nextLine();
lineNr++;
}
System.out.println(“已成功加载单词列表”);
随机r=新随机();
字符串解决方案=单词[r.nextInt(lineNr)];
System.out.println(“Solution=“+Solution”);
对于(int i=0;i您已经有了其中的关键成分:indexOf()
创建空格的数量有点棘手:创建一个与indexOf相同的数量与我们需要的正好相反。首先,我们必须计算最高的indexOf,这样我们就可以在每个单词前面创建减去当前单词indexOf的空格数量
我们必须记住单词,因为我们在整个循环中经过两次
下面的解决方案有点脏-更好的方法是为随机词的实例创建一个新类(使用小写版本和indexOf),这还可以保存一个有效的indexOf位置列表,这样您就不会总是使用第一次出现的字符
它只是一块踏脚石。还有很多事情要做,例如,你可以决定只使用小写单词,然后在最终输出中将“热门”字符改为大写
这段代码忽略了大写/小写,因此,如果您的解决方案单词以大写字符开头,您不会被锁定在某些随机单词中。在这里实现这一点的方式也是肮脏的
顺便说一句,加载列表可以大大简化,如下所示。这还可以避免不必要的大单词列表数组(否则有时可能太小)
你能分享你正在使用的测试文件吗?@Wisthler刚刚分享过it@ChrisK25所以你基本上想把答案词安排在适当的纵横填字游戏中吗?@Wisthler是的,这就是我想要的。这是一个可能的解决方案,但它与我的程序完全不同。我们的老师说这很简单,我们只需要改变一些东西。/但是谢谢谢谢你的时间!!!
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