gpt4 book ai didi

java - 将文件内容存储到数组中

转载 作者:行者123 更新时间:2023-12-01 15:12:41 25 4
gpt4 key购买 nike

我的刽子手程序有问题。我真的认为我需要做的事情超出了我对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);
}
}

我能够让程序读取文件,然后打印到屏幕,但我不知道如何将文件中的单词存储到数组中。我根本没有进步,所以请尝试尽可能彻底。

最佳答案

1)到目前为止,您所得到的看起来相当不错:)

2)由于您不知 Prop 体有多少个单词,因此您不需要“数组”。使用“ArrayList”可能会更好。数组是“固定的”。列表是“可变的”。

3)对于您阅读的每个“单词”,只需“.add()”将其添加到您的数组列表

瞧!完成。

这是一个完整的示例:

关于java - 将文件内容存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12113490/

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