Android 单击按钮后来自文件的随机字符串
Android Random string from file after button click
我对 Random 有一些疑问。我需要从 txt 文件中选择一行并在单击按钮后将其传递到 textview(每次不同的行)。我的代码在第一次点击时只执行一次,但我想为每个按钮点击显示下一行。 (抱歉我的英语不好)-_-
public class MainActivity extends Activity {
long seed = System.nanoTime();
String newString1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
valueA();
Button buttonGenerate = (Button)findViewById(R.id.button1);
final TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
valueA();
textGenerateNumber.setText(newString1);
}});
}
public void valueA (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
Random r = new Random(seed);
String a = list.get(r.nextInt(list.size()));
newString1 = a.substring(2);
}
}
第二题:
public class MainActivity extends Activity {
String newString1;
String newString2;
String newString3;
Random random = new Random(System.nanoTime());
.....
public void valueA (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
String a = list.get(random.nextInt(list.size()));
newString1 = a.substring(2);
}
public void valueB (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.answer));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
String b = list.get(random.nextInt(list.size()));
newString2 = b.substring(2);
}
你的seed是你的MainActivity时创建的固定值。因此
new Random(seed)
将始终生成相同的值。不要使用 seed 作为实例变量,而是使用 random
Random random = new Random(System.nanoTime());
并在 valueA() 方法中丢失新的随机行。
这样您就有了一个 Random 实例,并使用它来计算连续的 nextInt 值,而不是每次使用它时都生成一个新的 Random。
编辑:
要回答第二个问题,您最好在 newQuestion() 方法中同时生成问题和答案,然后使用 getQuestion 和 getAnswer 检索它们。这意味着您的 valueA 和 valueB 永远不会不一致。
String question;
String answer;
void newQuestion() {
int rval = random.nextInt();
question = list.get(rval);
answer = list.get(rval);
}
String getQuestion() {
return(question);
}
String getAnswer() {
return(answer);
}
就像 Neil Masson 说的那样,做一个全局随机数,因为就像你做的那样,它会给你总是相同的结果。所以创建一个 Random
private Random random = new Random();
random.setSeed(System.nanoTime());
然后在 Your Value 方法中,获取随机数。但是你不需要 valueA() 和 valueB(),一个就足够了 question/answer:
public void value (){
Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();
try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
s.close();
}
int nextInt = random.nextInt(questionList.size());
String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);
yourAnswerString = answerString.substring(2);
yourQuestionString = questionString.substring(2);
}
我对 Random 有一些疑问。我需要从 txt 文件中选择一行并在单击按钮后将其传递到 textview(每次不同的行)。我的代码在第一次点击时只执行一次,但我想为每个按钮点击显示下一行。 (抱歉我的英语不好)-_-
public class MainActivity extends Activity {
long seed = System.nanoTime();
String newString1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
valueA();
Button buttonGenerate = (Button)findViewById(R.id.button1);
final TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
buttonGenerate.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
valueA();
textGenerateNumber.setText(newString1);
}});
}
public void valueA (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
Random r = new Random(seed);
String a = list.get(r.nextInt(list.size()));
newString1 = a.substring(2);
}
}
第二题:
public class MainActivity extends Activity {
String newString1;
String newString2;
String newString3;
Random random = new Random(System.nanoTime());
.....
public void valueA (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
String a = list.get(random.nextInt(list.size()));
newString1 = a.substring(2);
}
public void valueB (){
Scanner s = new Scanner(getResources().openRawResource(R.raw.answer));
ArrayList<String> list = new ArrayList<String>();
try {
while (s.hasNextLine() ) {
list.add(s.nextLine());
}
} finally {
s.close();
}
String b = list.get(random.nextInt(list.size()));
newString2 = b.substring(2);
}
你的seed是你的MainActivity时创建的固定值。因此
new Random(seed)
将始终生成相同的值。不要使用 seed 作为实例变量,而是使用 random
Random random = new Random(System.nanoTime());
并在 valueA() 方法中丢失新的随机行。
这样您就有了一个 Random 实例,并使用它来计算连续的 nextInt 值,而不是每次使用它时都生成一个新的 Random。
编辑:
要回答第二个问题,您最好在 newQuestion() 方法中同时生成问题和答案,然后使用 getQuestion 和 getAnswer 检索它们。这意味着您的 valueA 和 valueB 永远不会不一致。
String question;
String answer;
void newQuestion() {
int rval = random.nextInt();
question = list.get(rval);
answer = list.get(rval);
}
String getQuestion() {
return(question);
}
String getAnswer() {
return(answer);
}
就像 Neil Masson 说的那样,做一个全局随机数,因为就像你做的那样,它会给你总是相同的结果。所以创建一个 Random
private Random random = new Random();
random.setSeed(System.nanoTime());
然后在 Your Value 方法中,获取随机数。但是你不需要 valueA() 和 valueB(),一个就足够了 question/answer:
public void value (){
Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();
try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
s.close();
}
int nextInt = random.nextInt(questionList.size());
String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);
yourAnswerString = answerString.substring(2);
yourQuestionString = questionString.substring(2);
}