Java 为什么不';难道我的话不少于8个字符吗? 公共字符串compWord()引发IOException,ClassNotFoundException { //局部常数 最终整数最大计数=8; //局部变量 BufferedReader=new BufferedReader(new FileReader(“dictionary.txt”);//创建一个新的BufferedReader,查找dictionary.txt List lines=new ArrayList();//新建ArrayList以跟踪行 字符串行;//当前行 Random rand=new Random();//新建随机对象 字符串字;//计算机的字 /*********************开始编译*********************/ //开始读取txt文件 line=reader.readLine(); //而行不是空的 while(行!=null) { //将该行添加到“行”列表中 行。添加(行); //转到下一行 line=reader.readLine(); } //将计算机单词设置为列表中的随机单词 word=lines.get(rand.nextInt(lines.size()); if(word.length()>最大计数) compWord(); //返回计算机的单词 返回词; }
据我所知,它应该只返回少于8个字符的单词?知道我做错了什么吗?if语句应该调用compWord,直到单词少于8个字符。但出于某种原因,我仍然会收到10-15个字符的单词。看看下面的代码:Java 为什么不';难道我的话不少于8个字符吗? 公共字符串compWord()引发IOException,ClassNotFoundException { //局部常数 最终整数最大计数=8; //局部变量 BufferedReader=new BufferedReader(new FileReader(“dictionary.txt”);//创建一个新的BufferedReader,查找dictionary.txt List lines=new ArrayList();//新建ArrayList以跟踪行 字符串行;//当前行 Random rand=new Random();//新建随机对象 字符串字;//计算机的字 /*********************开始编译*********************/ //开始读取txt文件 line=reader.readLine(); //而行不是空的 while(行!=null) { //将该行添加到“行”列表中 行。添加(行); //转到下一行 line=reader.readLine(); } //将计算机单词设置为列表中的随机单词 word=lines.get(rand.nextInt(lines.size()); if(word.length()>最大计数) compWord(); //返回计算机的单词 返回词; },java,list,arraylist,Java,List,Arraylist,据我所知,它应该只返回少于8个字符的单词?知道我做错了什么吗?if语句应该调用compWord,直到单词少于8个字符。但出于某种原因,我仍然会收到10-15个字符的单词。看看下面的代码: public String compWord() throws IOException, ClassNotFoundException { // Local constants final int MAX_COUNT = 8; // Local variables Buffer
public String compWord() throws IOException, ClassNotFoundException
{
// Local constants
final int MAX_COUNT = 8;
// Local variables
BufferedReader reader = new BufferedReader(new FileReader("dictionary.txt")); // Create a new BufferedReader, looking for dictionary.txt
List<String> lines = new ArrayList<String>(); // New ArrayList to keep track of the lines
String line; // Current line
Random rand = new Random(); // New random object
String word; // The computer's word
/********************* Start compWord *********************/
// Start reading the txt file
line = reader.readLine();
// WHILE the line isn't null
while(line != null)
{
// Add the line to lines list
lines.add(line);
// Go to the next line
line = reader.readLine();
}
// Set the computers word to a random word in the list
word = lines.get(rand.nextInt(lines.size()));
if(word.length() > MAX_COUNT)
compWord();
// Return the computer's word
return word;
}
如果选取的单词长度超过您的限制,则递归调用compWord
——但忽略返回值,只返回“太长”单词
我个人建议您避免递归,而只使用do
/while
循环:
if(word.length() > MAX_COUNT)
compWord();
return word;
或者,在阅读行的同时,提前筛选:
String word;
do
{
word = lines.get(rand.nextInt(lines.size());
} while (word.length() > MAX_COUNT);
return word;
while(行!=null){
如果(line.length如果单词长度超过8个字符,您只需再次调用您的方法,继续,并且没有任何更改
因此:
- 你从档案里找到了所有的字
- 然后从列表中获取一个随机单词,并将其放入
word
字符串中
- 如果单词长度超过8个字符,该方法将再次运行
- 但是,在最后,它总是返回它首先选择的单词。问题是,你只是递归调用该方法,而你对返回值不做任何处理。你调用一个方法,它会做一些事情,调用方方法将继续,在这种情况下,返回你的
单词
。如果他的方法是递归的还是非递归的
相反,我建议您使用Skeet推荐的非递归解决方案,或者学习一点关于递归的知识以及如何使用它。但是compWord是否应该在返回任何内容之前重新启动?@tdrunner95:是的,它会重新启动,选择一个随机字,可能再次递归,然后返回一个字。然后忽略返回值,即nd返回第一次选择的内容。我想你需要读一读递归……谢谢。我的教授几乎没有涉及递归。我将进一步研究它。@tdrunner95:递归是你目前正在做的事情,但这个问题实际上并不需要递归。你应该试着理解为什么你当前的代码不工作,然后解决这个问题用不同的方法解决问题,而不是使递归形式工作。
while(line != null) {
if (line.length <= MAX_COUNT) {
lines.add(line);
}
line = reader.readLine();
}
return lines.get(rand.nextInt(lines.size()));