简体   繁体   English

Android / Java:导入文本文件并从文本文件中读出随机行

[英]Android/Java: Import text file and read out random lines from within the text file

I got this code I've been working on so far, it is going to be a game im trying to create which just switches between two players. 我得到了这个代码我到目前为止一直在努力,它将成为一个游戏,我试图创建只是在两个玩家之间切换。 Every time it switches it is supposed to write out a question, like truth or dare. 每次切换它都应该写出一个问题,比如真理或者敢。 It is not allowed to write the same question twice, and should therefore be able to see if it has already used that question. 不允许两次写同一个问题,因此应该能够看出它是否已经使用过该问题。

So far I've got it running, and it switches between the two players every time you hit Next. 到目前为止,我已经运行了,每次你点击Next时它会在两个玩家之间切换。

But what I have a lot of trouble with, is fetching the data from within the txt file called man, here there is three lines, text1 text2 and text3. 但是我遇到了很多麻烦,就是从名为man的txt文件中获取数据,这里有三行,text1 text2和text3。 It should be able to take these randomly and know if it has already read one. 它应该能够随机取出并知道它是否已经读过一个。 I can not get the current InputStream to work, it says the man file is int, but it contains string? 我无法让当前的InputStream工作,它说man文件是int,但它包含字符串?

Here is the code so far: 这是迄今为止的代码:

package truthordare;

import java.io.FileInputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
//import android.content.Intent;


public class truthordareActivity extends Activity 

{
    public int i = 0;
    //public String man;
    //public String woman;
    TextView w;
    TextView m;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   


        {
        final Button buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(new View.OnClickListener() 
            {
                public void onClick(View v) 
                {
                gameCode();
                }
        });
    }
}


    /*public FileInputStream openFileInput(int man) throws IOException
{
    String collected = null;
    try 
    {
        FileInputStream fis = openFileInput(R.raw.man);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1)
            {
            collected = new String(dataArray);
            }
        fis.close();
    }
    catch (IOException e) 
        {
            e.printStackTrace();
        }
    return null;

}
*/



public void gameCode()
    {
        // Perform action on click
        setContentView(R.layout.game);
        if(i == 0)
            {
                w = (TextView)findViewById(R.id.textView1);
                w.setText("This is a player1");
                i = 1;
                final Button buttonNext = (Button) findViewById(R.id.buttonNext);
                buttonNext.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v) 
                    {
                        gameCode();
                    }
                });
            }
        else
            {
                m = (TextView)findViewById(R.id.textView1);
                m.setText("This is player2");
                i = 0;
                final Button buttonNext = (Button) findViewById(R.id.buttonNext);
                buttonNext.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v) 
                    {
                        gameCode();
                    }
                });
           }

    }  

}

Grab an InputStream using Resources.openRawResource . 使用Resources.openRawResource InputStream In your case this is getResources().openRawResource(R.raw.man) . 在你的例子中,这是getResources().openRawResource(R.raw.man)

Have you considered using an XML-file for your questions? 您是否考虑过使用XML文件来解决问题? From the information you provided, the structure should be like this: 根据您提供的信息,结构应如下所示:

<questions>
   <question>
      <id>1</id>
      <text>This is question nr. 1</text>
   </question>
   <question>
      <id>2</id>
      <text>This is question nr. 2</text>
   </question>
   <question>
      <id>3</id>
      <text>This is question nr. 3</text>
   </question>
</questions>

Load all the questions into a List/ArrayList as Question-objects and when a question is asked - remove it from the list. 将所有问题作为Question-objects加载到List / ArrayList中,并在询问问题时将其从列表中删除。 If you need to save the questions for later, don't remove them but rather save the ID's of all asked questions in another list, and when you try to get the next question make sure its ID is not in the list of ID's. 如果您需要保存问题以供日后使用,请不要删除它们,而是将所有问题的ID保存在另一个列表中,当您尝试获取下一个问题时,请确保其ID不在ID列表中。

To get a random question you just use a random number generator that provides you with values between 0 and list.size(). 要获得一个随机问题,您只需使用一个随机数生成器,它为您提供0到list.size()之间的值。

This way you won't have to spend time opening and closing InputStreams all the time and you have an easy way of making sure that a question is only asked once. 这样您就不必花时间一直打开和关闭InputStreams,并且您可以轻松确保只询问一次问题。

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

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