**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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.