问:我收到以下错误:
糟糕,您似乎没有审查(2a)这个词。mCensoredWords包含“ dork”,但是它以某种方式过去了。
com / teamtreehouse / Main.java
包 com.teamtreehouse ;
导入java.io.IOException ; 公共类Main {公共静态void main (String [] args ){ Prompter hinter = new Prompter (); 字符串故事= null ; 尝试{故事=提示器。newStoryTemplate (); } catch (IOException e ){ e 。
printStackTrace ();
}模板tmpl = new模板(故事);提示器。运行(tmpl ); } }
com / teamtreehouse / Prompter.java
包 com.teamtreehouse ;
导入java.io. * ; 导入java.nio.file.Files ; 导入java.nio.file.Path ; 导入java.nio.file.Paths ; 导入java.util.ArrayList ; 导入java.util.HashSet ; 导入java.util.List ; 导入java.util.Set ; 公共类Prompter { private BufferedReader mReader ; 私人Set < String > mCensoredWords ; 上市
提示器() {
的MReader =新的BufferedReader (新的InputStreamReader (系统。在)); loadCensoredWords (); } private void loadCensoredWords (){ mCensoredWords = new HashSet < String >(); 路径文件=路径。get (“ resources” ,“ censored_words.txt” ); 列表<字符串>单词= null
;
尝试{单词=文件。readAllLines (file ); } catch (IOException e ){系统。出来。println (“无法加载检查词” );e 。printStackTrace (); } mCensoredWords 。addAll (单词); } public void run (模板tmpl ){列表<字符串>
结果 = 空;
尝试{ results = promptForWords (tmpl ); } catch (IOException e ){系统。出来。println (“提示单词时出现问题” );e 。printStackTrace (); 系统。退出(0 ); } String finalString = tmpl 。渲染(结果); 系统。
出来。printf (“您的TreeStory:%n%n%s” , finalString );
} / ** * *提示输入新故事模板 * @return字符串 * @throws IOException * / public String newStoryTemplate ()引发IOException { String newStory ; 系统。出来。println (“输入新的故事模板:” );newStory = mReader 。readLine (); 返回newStory ;
}
/ ** *提示用户输入每个空格 * * @param tmpl编译后的模板 * @return * @throws IOException * / public List < String > hintForWords (Template tmpl )抛出IOException { List < String > words = new ArrayList <字符串>(); 对于(字符串短语:TMPL 。getPlaceHolders ()){字符串
字 = hintForWord (短语);
话。加(字); }返回的话; } / ** *提示用户输入空白。保证值不在被检查词列表中。 * * @param短语应该提示用户的单词。例如:形容词,专有名词,名称 * @return用户响应的内容 * / public String hintForWord (字符串短语)抛出IOException { System 。
出来。printf (“请输入%s:%n” ,短语);
字符串响应= mReader 。readLine (); 对于(字符串censoredWords :mCensoredWords ){如果(响应。等于(censoredWords )){系统。出来。printf (“%s是被检查的单词。请尝试使用其他单词。%n” ,响应);响应= mReader 。
readLine ();
} }返回响应; } }
伪测试
# 本质上,这就是我正在测试的内容
1. 提示用户输入新的字符串模板(其中带有双下划线的模板)。
一个。提示器类具有提示故事模板的新方法,并且该方法被调用。
2. 然后,提示用户输入每个被双下划线强调的单词。
一个。检查答案以查看其是否包含在检查的单词中。
持续提示用户,直到他们输入有效的单词
3。 向用户显示完整的故事。
答:问题出在这里:
公共 字符串 promptForWord (串 词) 抛出 IOException异常{
系统。出来。printf (“请输入%s:%n” ,短语);字符串响应= mReader 。readLine (); 对于(字符串censoredWords :mCensoredWords ){如果(响应。等于(censoredWords )){系统。出来。打印
(“%s是被删节的单词。请尝试使用其他单词。%n” , 响应);
响应= mReader 。readLine (); } }返回响应; }
我们实际上不必遍历任何事物。我们只需要检查我们的mCensoredWords是否包含我们键入的单词。我们还可以使用while语句。让我们看一下:
而 (mCensoredWords 。包含(entryWord 。toLowerCase ()))
在这里,我们检查mCensoredWords是否包含我们键入的单词。我们使用toLowerCase(),因为如果您查看被检查的单词文本文件,则全部使用小写字母。如果我们输入大写单词,那么我们的坏单词会通过,这不好。因此,尽管我们的输入是一个坏词,但我们一直要求他们输入一个词。您在if语句中写的内容是正确的。就其余的代码而言,这很好。我已附上我的代码作为参考:
公共的 字符串的 提示(字符串的 短语) 抛出 IOException {
System 。出来。printf (“请输入您的%s单词:%n” ,短语); 字符串entryWord = mReader 。readLine (); 而(mCensoredWords 。包含(entryWord 。toLowerCase ())){系统。出来。printf (“很好,请为%s再输入一个单词。%n”
, 短语);
entryWord = mReader 。readLine (); }返回entryWord 。修剪(); }
如果这样做有帮助,请不要忘记将其标记为最佳答案,以便其他有相同问题的人也可以看到。