我得到了這段代碼,我一直在努力,它將會是一個遊戲,我試圖創建只是在兩個玩家之間切換。每次它切換它應該寫出一個問題,如真相或敢。不允許兩次寫同一個問題,因此應該能夠看到它是否已經使用過這個問題。Android/Java:導入文本文件並從文本文件中讀出隨機行
到目前爲止,我已經運行了它,並且每次點擊下一步時它都會在兩個玩家之間切換。
但是我有很多的麻煩,從所謂的男人txt文件中獲取數據,這裏有三條線,文本1文本2和文字3。它應該能夠隨機取出並知道它是否已經讀過。我不能讓當前的InputStream工作,它說man文件是int,但它包含字符串?
這裏是到目前爲止的代碼:使用Resources.openRawResource
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();
}
});
}
}
}
我已經設法在xml文件中創建一個字符串數組並讀取並顯示它。我將如何製作該功能,以便它不會採用兩次相同的問題? – psalomonsen
這是我現在得到的samle代碼(我做了一個測試項目,當我得到它的時候我會實現它)只需點擊鏈接。 [鏈接](http://pastebin.com/JWhK8cuZ) – psalomonsen
您應該將問題加載到一個List/ArrayList而不是一個數組中。這樣你可以在閱讀完問題後使用列表中的'remove()'方法,並且你不會得到兩次相同的問題。 – kaspermoerch