[英]how to make a random question without repeating it?
**So I have to make a random question for my logic quiz and I have a written code here. **所以我必须为我的逻辑测验做一个随机问题,我在这里有一个书面代码。 I do not know what is the syntax for java because I just started learning it and I started with kotlin so it is a bit unfamiliar to me.我不知道 java 的语法是什么,因为我刚开始学习它,我是从 kotlin 开始的,所以对我来说有点陌生。 Here is the code: **这是代码:**
Random r;
String [] quest = {"Which alphabet will be 14th to the left of 8th alphabet from the right in the following series of letters?" +
"A O B P C Q D R E S F T G U H V I W J X K Y L Z M N.", "Hritik is taller than Salman who is shorter than Sanjay. Akshay is taller than " +
"Shahrukh but shorter than Salman. Sanjay is shorter than Hritik. Who is the tallest?" , "Lali and Anju are a married couple. Tunu and Munu " +
"are brothers. Tunu is the brother of Lali. How is Munu related to Anju?", "How many sets of two letters have as many letters " +
"between them as in the English alphabetical order in the word ‘WRISTWATCH’."};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy);
Button question = findViewById(R.id.btnQuestion);
final TextView ask = findViewById(R.id.txtAsk);
r = new Random();
question.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ask.setText(quest[r.nextInt(quest.length)]);
}
});
}
Do it as follows:请按以下步骤操作:
Random r = new Random();
String [] quest = {"Which alphabet will be 14th to the left of 8th alphabet from the right in the following series of letters?" +
"A O B P C Q D R E S F T G U H V I W J X K Y L Z M N.", "Hritik is taller than Salman who is shorter than Sanjay. Akshay is taller than " +
"Shahrukh but shorter than Salman. Sanjay is shorter than Hritik. Who is the tallest?" , "Lali and Anju are a married couple. Tunu and Munu " +
"are brothers. Tunu is the brother of Lali. How is Munu related to Anju?", "How many sets of two letters have as many letters " +
"between them as in the English alphabetical order in the word ‘WRISTWATCH’."};
boolean [] usedIndices = new boolean[quest.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy);
Button question = findViewById(R.id.btnQuestion);
final TextView ask = findViewById(R.id.txtAsk);
question.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int indextoBeUsed;
do {
indextoBeUsed = r.nextInt(quest.length);
} while (usedIndices[indextoBeUsed]);
usedIndices[indextoBeUsed] = true;
ask.setText(quest[indextoBeUsed]);
}
});
}
The solution is quite straight forward.解决方案非常简单。 Feel free to comment in case of any doubt.如有任何疑问,请随时发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.