gpt4 book ai didi

java - 从文本文件中选择一个随机单词

转载 作者:行者123 更新时间:2023-12-01 16:17:53 28 4
gpt4 key购买 nike

我正在尝试开发一个刽子手作为一项作业,但无法从文本文件中获取一个随机单词(该文件有各种单词,每个单词都用空格分隔)。我编写了一段代码来获取一个随机单词,但无法选择一个单词并将其替换为“Function()”中的示例字符串 (String w = "this";)。

 public String randomWord(String wordran) {

try {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt"));
String line = reader.readLine();
List<String> words = new ArrayList<String>();
while (line != null) {
String[] wordline = line.split(" ");
for (String word : wordline) {
words.add(word);
}
Random rand = new Random();
String randomWord1 = words.get(rand.nextInt(words.size()));
//System.out.println("rand word : " + randomWord1);

}
reader.close();

} catch (Exception e) {

}
return wordran;


}


public void function(){

int numGuesses = 10;
String w = randomWord();

String[] word = w.split("");
ArrayList< String> wList = new ArrayList<>(Arrays.asList(word));
ArrayList< String> wAnswer = new ArrayList< String>(wList.size());
for (int i = 0; i < wList.size(); i++) {
wAnswer.add("_ ");
}
int left = wList.size();
Scanner scanner = new Scanner(System.in);
boolean notDone = true;
ArrayList< String> lettersGuessed = new ArrayList< String>();

while (notDone) {
System.out.println();
String sOut = "";

List< String> lettersLeft = getRemainingLetters(lettersGuessed);
for (String s : lettersLeft) {
sOut += s + " ";
}
System.out.println("Letters Left: " + sOut);

sOut = "";
for (int i = 0; i < wList.size(); i++) {
sOut += wAnswer.get(i);
}
System.out.println(sOut + " Guesses left:" + numGuesses);
System.out.print("Enter a letter(* exit): ");
String sIn = scanner.next();
numGuesses--;
if (sIn.equals("*")) {
break;
}
lettersGuessed.add(sIn);
for (int i = 0; i < wList.size(); i++) {
if (sIn.equals(wList.get(i))) {
wAnswer.set(i, sIn);
left--;
}
}
if (left == 0) {
System.out.println("Congradulations you guessed it!");
break;
}
if (numGuesses == 0) {
System.out.println("You failed...:(");
break;
}

}

}

public static void main(String[] args) throws IOException {
Main ma = new Main();

ma.function();

loadWords();

// ma.randomWord();

}

最佳答案

您的代码存在三个问题:

  1. 您不需要传递参数String wordran来存储随机单词。一个有用的参数可以是字符串路径,通过它您可以将文件的路径传递给函数。
  2. 您错过了在循环中读取文件内容的机会。您刚刚阅读了第一行。
  3. 您尚未返回通过应用 Random#nextInt 计算出的随机单词。

顺便说一句,我建议您使用 try-with-resources语法来摆脱显式关闭 BufferedReader 的情况。

下面给出的是包含这些注释的正确代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {
public static void main(String[] args) throws IOException {
// Test
System.out.println(getRandomWord("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Main\\words.txt"));
}

public static String getRandomWord(String path) throws IOException {
List<String> words = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = reader.readLine()) != null) {
String[] wordline = line.split("\\s+");
for (String word : wordline) {
words.add(word);
}
}
}
Random rand = new Random();
return words.get(rand.nextInt(words.size()));
}
}

关于java - 从文本文件中选择一个随机单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358012/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com