Android/Java:导入文本文件并从文本文件中读取随机行
我得到了到目前为止我一直在研究的代码,这将是我试图创建的一个游戏,它只是在两个玩家之间切换。每次切换时,它都应该写出一个问题,比如真心话大冒险。不允许将同一问题写两次,因此应该能够查看它是否已经使用过该问题。
到目前为止,我已经运行了它,并且每次您单击“下一步”时,它都会在两个播放器之间切换。
但我遇到了很多麻烦,是从名为 man 的 txt 文件中获取数据,这里有三行,text1、text2 和 text3。它应该能够随机读取这些内容并知道它是否已经读过。我无法让当前的 InputStream 工作,它说 man 文件是 int,但它包含字符串?
这是到目前为止的代码:
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();
}
});
}
}
}
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.
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. 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?
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();
}
});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 XML 文件来解决您的问题?根据您提供的信息,结构应该是这样的:
将所有问题作为问题对象加载到列表/数组列表中,当提出问题时 - 将其从列表中删除。如果您需要保存问题供以后使用,请不要删除它们,而是将所有提出的问题的 ID 保存在另一个列表中,并且当您尝试获取下一个问题时,请确保其 ID 不在 ID 列表中。
要获得随机问题,您只需使用随机数生成器,它为您提供 0 到 list.size() 之间的值。
这样,您就不必一直花费时间打开和关闭 InputStream,并且可以轻松地确保问题只被询问一次。
Have you considered using an XML-file for your questions? From the information you provided, the structure should be like this:
Load all the questions into a List/ArrayList as Question-objects and when a question is asked - remove it from the list. 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.
To get a random question you just use a random number generator that provides you with values between 0 and 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.
使用
Resources.openRawResource
获取InputStream
。在您的情况下,这是getResources().openRawResource(R.raw.man)
。Grab an
InputStream
usingResources.openRawResource
. In your case this isgetResources().openRawResource(R.raw.man)
.