【问题标题】:How do I get my Hangman Java file to read a random word in from a .txt file?如何让 Hangman Java 文件从 .txt 文件中读取随机单词?
【发布时间】:2016-04-14 15:41:42
【问题描述】:
char hangman[];
Scanner sc = new Scanner( System.in);
Random r = new Random();
File input = new File("ComputerText.txt").useDelimiter(",");
Scanner sc = new Scanner(input);
String words;

我想从 .txt 文件中读取一组单词,并让程序选择一个随机单词用于刽子手游戏。

下面的代码是用于当我们在代码中读取 .txt 文件时。 我们想使用三个不同的 .txt 文件,每个文件都有不同的类别,并让用户选择他们想要的词来自哪个类别。

//while(decision==1){word=computerWord;}
if ( decision == 1)
{
word=computerWord;
}
else if ( decision == 2)
{
word = countryWord;
}
else if (decision == 3)
{
word = fruitWord;
}
else
{
System.out.println("error, try again");
}

【问题讨论】:

  • 什么不起作用?你遇到问题了吗?也许创建一个读取流来从文件中读取? BufferedReader 应该可以解决问题。
  • 对不起 Bram,我使用 java 的时间不长,只知道基础知识。当我们这样写时,主程序正在读取单词: String[] word = {"program", "computer", "download", "database"};但我希望程序从文本文件中读取这些单词,并让文件使用随机类来选择一个单词
  • @sarahjane call sc.next();
  • Null Saint,您是指问题第二部分中的用户输入吗?就像 sc.next() 将允许用户从三个选项中选择一个?
  • @sarahjane 您已将扫描仪设置为从文件变量中读取,但为了从该文件中读取,请调用 sc.next()

标签: java file if-statement random


【解决方案1】:

以下是必须使用扫描仪类读取文件的方式:-

    try 
    {
        Scanner input = new Scanner(System.in);
        File file = new File("ComputerText.txt");

        input = new Scanner(file);

        String contents;
        while (input.hasNext()) 
        {
            contents = input.next();
        }
        input.close();

    } 
    catch (Exception ex) 
    {
    }

此时所有文件内容都会在contetns变量中,然后你可以使用split方法根据你的分隔符进行分割

【讨论】:

    【解决方案2】:

    你的方法应该是:

    使用以下导入:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    

    制作返回所需单词的函数:

    if ( decision == 1)
    {
        word = getComputerWord();
    }
    else if ( decision == 2)
    {
        word = getCountryWord();
    }
    else if (decision == 3)
    {
        word = getFruitWord();
    }
    else
    {
        System.out.println("error, try again");
    }
    

    通过以下方式实现:

    public String getComputerWord() {
        return getRandomWordFromFile(computerWordsPath);
    }
    
    public String getCountryWord() {
        return getRandomWordFromFile(countryWordsPath);
    }
    
    public String getFruitWord() {
        return getRandomWordFromFile(fruitWordsPath);
    }
    
    //returns random word from ","-delimited file of words
    public String getRandomWordFromFile(String path) {
        String fileContent = readFileToString(path);
        String[] words = fileContent.split(",");
        Random rng = new Random();
        return words[rng.nextInt() % words.length];
    }
    
    
    //build string from file by simply concatenating the lines
    public String readFileToString(String path) {
        try { 
            BufferedReader br = new BufferedReader(new FileReader(path));
    
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
                while (line != null) {
                    sb.append(line);
                    line = br.readLine();
                }
                return sb.toString();
            } finally {
                br.close();
            }
        } catch (IOException ioe) {
            //Error handling of malformed path
            System.out.println(ioe.getMessage());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2017-08-30
      相关资源
      最近更新 更多