[英]how to random questions in android
I'm developing a simple quiz in my android project, and I have created 15 sample questions.我正在我的 android 项目中开发一个简单的测验,我已经创建了 15 个示例问题。 I want to randomise the questions, but I don't know how to write the code for that.
我想将问题随机化,但我不知道如何为此编写代码。 This is what I have so far
这是我到目前为止
package com.adm.kana;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class Kuis_Hiragana extends Activity implements View.OnClickListener {
TextView pertanyaan, salahbenar;
Button jawab, lanjut;
EditText jawaban;
int benar = 0, salah = 0, index = 0;
String[] soal = {"ぐ ", "く ", "あ ", "ば ", "き ゅ ", "ち ょ ", "ち ", "お ", "ぽ ", "ね ", "わ ", "む ", "れ ", "ぞ ", "し ょ "};
String[] jawabann = {"gu", "ku", "a", "ba", "kyu", "cho", "chi", "o", "po", "ne", "wa", "mu", "re", "zo", "sho"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.kuis);
salahbenar = (TextView) findViewById(R.id.salahbenar);
salahbenar.setText("");
pertanyaan = (TextView) findViewById(R.id.soal);
pertanyaan.setText(soal[0]);
lanjut = (Button) findViewById(R.id.lanjut_soal);
lanjut.setOnClickListener(this);
jawab = (Button) findViewById(R.id.btn_jawab);
jawab.setOnClickListener(this);
}
@Override
public void onClick(View v) {
salahbenar = (TextView) findViewById(R.id.salahbenar);
jawaban = (EditText) findViewById(R.id.jawab);
if (v == jawab) {
String jawab1 = jawaban.getText().toString();
if (jawab1.equalsIgnoreCase(jawabann[index])) {
salahbenar.setText("BENAR");
benar++;
}
else {
salahbenar.setText("SALAH");
salah++;
}
}
else if (v == lanjut) {
if (index < soal.length - 1) {
index++;
pertanyaan.setText(soal[index]);
jawab.setEnabled(true);
lanjut.setVisibility(View.VISIBLE);
jawaban.setText("");
salahbenar.setText("");
}
else {
Intent i = new Intent(Kuis_Hiragana.this, Hasil.class);
i.putExtra("BENAR", benar);
i.putExtra("SALAH", salah);
startActivity(i);
onStop();
System.exit(0);
}
}
}
}
Its quite simple.它很简单。 I will explain step-by-step.
我将逐步解释。
Step 1第1步
String[] questions = {"Question 1","Question 2","Question 3","Question 4"};
Add 15 questions like this to the string array.将这样的 15 个问题添加到字符串数组中。
Step 2第2步
Create an object of the Random class.创建 Random 类的对象。
Random randomQuests = new Random();
Step 3第 3 步
Generate a random question simply by using the rantInt method简单地使用 rantInt 方法生成一个随机问题
String randomQuestion = questions[randomQuests.nextInt(15)];
The parameter passed must be 15, so that it can generate random numbers between 0 to 14.传递的参数必须是15,这样才能生成0到14之间的随机数。
Hope this helps.希望这可以帮助。
If you want to get a random index I suggest you use java Random calss如果你想获得一个随机索引,我建议你使用 java Random calss
Random randomGenerator = new Random();
randomGenerator.nextInt(size of your array);
Now If you have stored your question in an array String you can use random function现在,如果您已将问题存储在数组 String 中,则可以使用随机函数
Random rand=new Random();
String[] soal={"1 question","2 Question","3 Question "," 15 question"};
String randomQuestion=soal[rand.nextInt(15)];
If you have 15 question then rand.nextInt(15) will provide random value from 0 to 14如果您有 15 个问题,那么 rand.nextInt(15) 将提供从 0 到 14 的随机值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.