簡體   English   中英

使用用戶輸入在文本文件中前進文本

[英]Advancing text in a text file using 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 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);

我希望自己朝着正確的方向前進,而不是舉一反三,因為我想自己弄清楚它,但是一點幫助將不勝感激!

Type Writer方法:

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());
}

對於單行打印,請等待x時間。 替換其中的代碼

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM