我的hangman程序有问题。我真的认为我需要做的事超出了我对Java的了解。这是我的代码
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Random; public class HangmanProject { public static void main(String[] args) throws FileNotFoundException { String scoreKeeper; // to keep track of score int guessesLeft; // to keep track of guesses remaining String[] wordList = new String[25]; final Random generator = new Random(); Scanner keyboard = new Scanner(System.in); // to read user's input System.out.println("Welcome to Nick Carfagno's Hangman Project!"); // Create a scanner to read the secret words file Scanner wordScan = null; try { wordScan = new Scanner(new BufferedReader(new FileReader("words.txt"))); while (wordScan.hasNext()) { } } finally { if (wordScan != null) { wordScan.close(); } } // get random word from array class pickRand { public String get(String[] wordList) { int rnd = generator.nextInt(wordList.length); return wordList[rnd]; } } System.out.println(wordList); } }
我能够使程序读取文件,然后打印到屏幕上,但是我不知道如何将文件中的单词存储到数组中。我一点都没有进步,所以请尝试并做到透彻。
您需要将读取的行保存在一个String对象中,并将其分配给数组的某个字段。例如:
String
wordList[0] = myString;
这会将值赋给myString数组的第一个字段。
myString