- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我基本上是在写一个刽子手游戏,我让程序从我的 .txt 文件中读取这四个字母的单词。我已将 .class 和 .txt 文件复制到程序的 src 文件夹中,它仍然打印出“找不到文件”。我在我的 RandomWordProvider 类中写的。这是代码:
public class WordGuessingGame {
static class RandomWordProvider {
private final List<String> words;
public RandomWordProvider() {
words = readFile();
}
private int randomInteger() {
int randomInt = (int) (Math.random() * words.size());
return randomInt;
}
public String getWord(int randomInt, String line) {
int randomPosition = randomInteger();
String randomWord = words.get(randomPosition);
return randomWord;
}
private List<String> readFile() {
List<String> wordsList = new ArrayList<>();
try {
String fileName = "FourLetterWords.txt";
File fourLetterWords = new File(fileName);
Scanner in = new Scanner(fourLetterWords);
while (in.hasNextLine()) {
String line = in.nextLine();
if (line!=null && !line.isEmpty()) {
wordsList.add(line);
}
}
} catch (FileNotFoundException ex) {
System.out.println("File not found.");
}
return wordsList ;
}
}
static class PlayerCharacterEntry {
public String playerEntry() {
Scanner characterEntry = new Scanner(System.in);
System.out.print("Enter a character: ");
String playerInput = characterEntry.next();
playerInput = playerInput.toUpperCase();
return playerInput;
}
}
public static void main(String[] args) {
Scanner wantToPlay = new Scanner(System.in);
System.out.print("Welcome to the word guessing game! Would you like to play? ");
String playerAnswer = wantToPlay.next();
if (playerAnswer.equals("Yes") || playerAnswer.equals("yes")) {
System.out.print("\nYour objective is to guess a four letter word by entering"
+ "\nletters on your keyboard. If you can not guess the word in seven attempts,"
+ "\nyou lose! You will be told if the letter you entered is in the word, and"
+ "\nyou will be told if the letter you entered is not in the word. You will be"
+ "\nallowed to guess the word any time during your seven attempts. If at anytime"
+ "\nyou would like to terminate the game, enter the word 'terminate'. Good Luck!");
}
if (playerAnswer.equals("No") || playerAnswer.equals("no")) {
System.out.print("Maybe another time!");
}
RandomWordProvider randomWordProvider = new RandomWordProvider();
PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();
String playerInput;
String randomWord;
}
}
很明显它在做“尝试”,但它没有工作,它只是在做找不到的文件,但我不知道为什么。有人可以帮助我吗?
最佳答案
如果您不想指定硬编码路径,这里是从您的类路径获取文件的想法:
try {
URL url = RandomWordProvider.class.getResource("FourLetterWords.txt");
File fourLetterWords = new File(url.toURI());
Scanner in = new Scanner(fourLetterWords);
....
}
关于java - 当我的文件位于正确的文件夹中时,为什么我一直从我的 catch 异常中获取 "File not found."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27551983/
我有一个在 Android 市场上相当流行的应用程序,它允许数以万计的用户按下一个按钮并向它发出语音命令。然后我就可以做很多不同的事情,比如给他们提供当前的天气预报等等...... 无论如何,我的应用
令人惊讶的是,标题基本上解释了它。我们有一个我们的客户制作的页面,我们正在重新创建该页面。 页面高度会一直增加,直到(我假设是这样)浏览器达到它的极限。我已经尝试过 Firebug 和 W3 验证器,
我是 react-native 的新手,试图创建我自己的组件,但它一直显示一个空屏幕。 这是我的组件代码 class BoxComponent extends Component { cons
我正在为我的 PHP 元素创建一个非常简单的博客,但遇到了一个简单的问题。我无法让我的页眉图像一直 float 。我有一个横幅,左边有一些文字,我有一个 1px 的切片,在可以选择的任何分辨率的宽度上
为什么我可以在另一个 Controller 的 View 中访问一个 Controller 的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它? 最佳答案 @George Schreib
我正在使用带有最新 ADT 插件的 Eclipse Kepler SP2。每隔一分钟 Eclipse 就会说“为 Android 4.4.2 加载数据”并阻止我想做的一切。我在不同的文件夹中有几个 E
我是一名优秀的程序员,十分优秀!