Java 使用用户输入推进文本文件中的文本

Java 使用用户输入推进文本文件中的文本,java,text,user-input,Java,Text,User Input,因此,在我大学第一年的圣诞计划中,我需要使用if语句制作一个基本游戏,我选择了文本冒险,但作为一名新手,我选择了写出每一份打印出来的内容: System.out.println("You approach the two cupboards one on the right and one on the left.\nWhich one will you inspect?"); System.out.println("Please press 1 to inspect the r

因此,在我大学第一年的圣诞计划中,我需要使用if语句制作一个基本游戏,我选择了文本冒险,但作为一名新手,我选择了写出每一份打印出来的内容:

System.out.println("You approach the two cupboards one on the right and one on the left.\nWhich one will you inspect?");
        System.out.println("Please press 1 to inspect the right cupboard or 2 to press the left cupboard.");
        int Action1 = 0;
        while(Action1 !=1 && Action1!=2){//Checking to make sure no one puts in a number thats not 1 or 2
            Action1 = in.nextInt();//Start of Player choice
            if( Action1 == 1)
            {
                System.out.println("In the right cupboard you find a note.\nUpon reading the note it says:");
                System.out.println("Hello" + " " + name + " " + "you're probably wondering why you woke up in this kitchen whith driend blood on your shirt unable to rememeber anything.\nPretty understandable. However, you're a part of my game now and I expect you to play by the rules. I'm waiting for you in the attic, if you can get here in one piece I'll reveal all.\n Ciao");
                Enter = in.nextLine();
                System.out.println("After reading the note you check the next cupboard.");
                System.out.println("It contains a small key.\nFar too small for any of the locks in the kitchen, you keep it and move on.");
                Enter = in.nextLine();
            }
但是现在我暑假休假了,我正在努力自学一些编程,以免生锈,我想回到我的游戏中,通过将文本嵌入文本文件来清理它是个好主意,不过有一个问题,我完全不知道如何让用户以自己的速度推进文本当前所有文本都出现在屏幕上,看起来有点不和谐。我将包括一些示例代码,以防对您有所帮助

 Scanner in = new Scanner(System.in);
        FileReader file = new FileReader("C:/Users/jamie/Desktop/TextAdventure/TextFiles/Opening.txt");
        BufferedReader reader = new BufferedReader(file);

        String text = ""; //string to hold the text in the text file
        String line = reader.readLine();
        while (line != null)
        {
            text += line+"\n";
            line = reader.readLine();
        }
        System.out.println(text);
我更喜欢在正确的方向上轻推,而不是一个完整的例子,因为我想自己弄清楚,但一点帮助将是非常感谢

打字机方法:

String filename = "file.txt";
List<String> lines = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(filename)))
{
    String str;
    while ((str = br.readLine()) != null)
    {
        lines.add(str);
    }
}
catch (IOException ioe)
{
    //handle errors
}

while (lines.size() > 0)
{
    //we know that lines is more than 0 so remove the first element
    String nextLineOut = lines.remove(0);
    for (int charIndex = 0; charIndex < nextLineOut.length(); charIndex++)
    {
        System.out.print(nextLineOut.charAt(charIndex));
        try
        {
            //every time a character is printed to the console sleep for 50 milliseconds
            Thread.sleep(50);
        }
        catch (InterruptedException ie)
        {//handle errors
        }
    }
    //we have reached end of line so print out a new line
    System.out.print(System.lineSeparator());
}


我只是想澄清一下。当用户按任意键时,您希望只打印一行?是的,对不起,应该让Clear打印一行,然后等待用户按任意键(并告诉用户按任意键继续),然后在他们按任意键后删除该消息并打印下一行,这并不容易完成。原因:有两种方法可以很好地工作,一种是打字机方法(打印一个字符,等几毫秒,打印一个字符,等等),另一种是只打印一整行,等几秒钟,然后重复,如果你愿意的话,我可以用两种方法的一个简单例子来回答你的问题?当然,那太好了,谢谢!谢谢非常感谢!
while(lines.length() > 0)
{

}
String nextLineOut = lines.remove(0);
System.out.println(nextLineOut);
try
{
    //get amount of characters in the next line multiply by 50 to get a pause of x milliseconds per line
    Thread.sleep(nextLineOut.length() * 50);
}
catch (InterruptedException ie)
{//handle errors
}