简体   繁体   English

从java中的文本文件打印随机行

[英]Print random line from text file in java

I am new to java and for a class I am supposed to create a hangman style game.我是 Java 新手,对于一个班级,我应该创建一个刽子手风格的游戏。 First I am just trying to pull a random word from a txt file, however when I run the following code it just prints the full list.首先,我只是想从 txt 文件中提取一个随机单词,但是当我运行以下代码时,它只会打印完整列表。 Any suggestions?有什么建议? My teacher advised we cannot use arrays, we have to get a random int, then use it with nextLine to get the random word...我的老师建议我们不能使用数组,我们必须获得一个随机整数,然后将它与 nextLine 一起使用以获取随机词...

Also, ignore the random stated variables, thats for later in the assignment, but don't want to ask on that unless needed later!另外,忽略随机声明的变量,这在分配的后面,但不要问这个,除非以后需要!

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class WordGame {

static Random randomNum = new Random();

   public static void main(String[] args) throws IOException
   {
   
   int numberGames = 1;
   int highScore;
   int avgScoreRunning;
   int avgScoreFinal;
   int currentScore;
   String secretWordFinal = "";
   
   System.out.print("Welcome to Words: The Word Guessing Game!\n");
   System.out.print("Play as many games as you like. I'll remember your top score.\n");
   System.out.println("and also compute your average for all games played.");
   
   findWord(secretWordFinal);
   }
      public static void findWord(String secret) throws IOException
      {
   
      File wordList = new File("wordlist.txt");
      Scanner wordListNew = new Scanner(wordList);
   
      int wordNumber = randomNum.nextInt(33735) +1;   
            
      for(int i = 1; i <= wordNumber;){
            if(i < wordNumber){
            i++;
            }
            else if (i == wordNumber){
            String secretWord = wordListNew.nextLine();
            System.out.println(secretWord);
            }

The answer boils down to your for loop condition and when you are calling nextLine() .答案归结为您的for循环条件以及您何时调用nextLine()

Starting off with nextLine() , you are currently only calling nextLine() when you reach the numbered line you want.nextLine() ,您目前只在到达所需编号的行时调用nextLine() However, you need to advance the "pointer" in the file to reach that line as well, otherwise, you will only read the first line of the file when you call it.但是,您还需要将文件中的“指针”前移以到达该行,否则,您在调用它时只会读取文件的第一行。 This is compounding with the next issue though:但是,这与下一个问题复杂化:

Second, because your for loop condition is i <= wordNumber , you are continuing to loop when you finally do get to that line number, but because you don't increment i again once you reach the number you want (you don't have i++ inside your else if code), it is always evaulating to true ( i == wordNumber ) and keeps looping.其次,因为你for循环条件是i <= wordNumber ,你是继续循环,当你终于得到该行号,但因为你不增加i ,一旦你达到你想要的数字再次(你没有i++在你的else if代码中),它总是计算为真( i == wordNumber )并保持循环。 You can see below I've put the incrementing of i into the third position of the for loop so that it always increments.您可以在下面看到我将i的增量放入for循环的第三个位置,以便它始终增量。

for(int i = 1; i <= wordNumber; i++){
    String secretWord = wordListNew.nextLine();
    if (i == wordNumber){
        System.out.println(secretWord);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM