gpt4 book ai didi

java - 刽子手游戏不从文件中读取

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:40 24 4
gpt4 key购买 nike

该程序应该从 .txt 文件中随机选择一个单词,并让用户尝试猜测该单词的字母。它运行了,但它总是说“在单词中找不到字母”,即使我知道这是所有单词都有的字母。这让我认为它没有正确读取我的 .txt 文件。

package hangman;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Hangman{



public static void main(String[] args) {

ArrayList<String> dictionaryList = new ArrayList<>();
File file = new File("src/hangman.txt");
try {
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
dictionaryList.add(line);
}
}
} catch (FileNotFoundException e) {
}

/*
* Getting the word and hiding it
*/

Random rng = new Random();
int word = rng.nextInt(dictionaryList.size()); //randomly chooses a word from the text file
Scanner scanner = new Scanner(System.in);
int guessesLeft = 6; // the total amount of guesses the user gets
ArrayList<Character> alreadyGuess = new ArrayList<>(); // keep tracks of the user's guesses
boolean wordFound = false; // keeps track of when the game will end after the user runs out of guesses


String wordSelected = dictionaryList.get(word); //converts the int value of the randomly choose word to a string
char[] letters = wordSelected.toCharArray(); // converts the word to a char
char[] hideWord = new char[letters.length]; // gets the length of hiding the word


// the for loop hides the word by replacing it with '_'
for(int i = 0; i < letters.length; i++) {
hideWord[i]='_';
}

/*
* Starts the hangman game. The while loop will keep running the game.
*/

while(true){

//for testing purposes they can use the print statement below to replace the other print statement
//System.out.print("\n" + wordSelected + "\n" + "Word: ");
System.out.print("\n" + "Word: ");
for(int i = 0; i < letters.length; i++){
System.out.print(hideWord[i]);
} // Display the word

// Allows user to input and displays the guesses that are left
System.out.print("\n" + "Guesses Left: " + guessesLeft +"\nAlready Guess: " + alreadyGuess + "\nGuess: ");
char userInput = scanner.nextLine().toUpperCase().charAt(0); // uppercase the String first, and then pick the char

// Checks to see if the user already guess the same word
for(int i = 0; i < alreadyGuess.size(); i++){
if(userInput==alreadyGuess.get(i)){
System.out.println("\nYou already guessed this letter. Try Again. ");
break;
}
}

// records the user's guesses
if(!(alreadyGuess.contains(userInput))){
alreadyGuess.add(userInput);
}


// Checks if the user guesses the right letter in the word
for(int i = 0; i < letters.length; i++) {
if(userInput==letters[i]) {
hideWord[i] = userInput;
wordFound = true;

}
}


// If user guesses the incorrect letter it will display this and lower the amount of guesses
if(!wordFound){
System.out.println("\nThe letter was not found in the word. \n");
guessesLeft = 1;
}


wordFound = false; // resets the wordFound boolean back to false

// if user runs out of guesses left, they will lose.
if(guessesLeft<=0){
System.out.println("\nYou lose, you hanged the man.");
break;
}

// if user guesses correctly on the word, they win. Uses the array class to compare two char arrays
if(Arrays.equals(letters,hideWord)){
System.out.println("\nWord: " + wordSelected);
System.out.println("\nCongratulations! You guess the right word and save the man from being hang.");
break;
}


}
}
}

最佳答案

永远、永远、永远捕获并忽略异常!更改程序顶部的 catch 子句,如下所示:

catch (FileNotFoundException e) { 
e.printStackTrace();
}

这样,当您尝试阅读单词列表时,如果发生了不好的事情,您至少会得到提示。

这是一个非常重要的教训,值得以粗体重复:永远、永远、永远不要捕获和忽略异常!

关于java - 刽子手游戏不从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838943/

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