此代码是我用来制作测验应用程序的代码...但问题是我不知道如何使其随机化或随机播放。我不知道要改变什么来让你洗牌你能帮助我吗?
package org.intercode.lifeatceu;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class levelone extends AppCompatActivity {
TextView tv1, tvCred, tvQuestion;
Button btNext;
RadioButton rb1, rb2;
RadioGroup rg;
String questions [] = {"Ma. Cristina D. Padolina is CEU's President", "Carlito B. Olaer is the V.P of CEU", "CEU's VISION is to sting every enemy", "One of CEU's Mission is to promote creative and scholarly academic"};
String answer [] = {"True", "False", "False", "True"};
int flag = 0;
public static int score, correct, wrong, coins;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levelone);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent inquiz = getIntent();
final int credit = inquiz.getIntExtra("passedCredits", 0);
String TotalCoins = String.valueOf(credit);
tv1 = (TextView) findViewById(R.id.tv1);
tvCred = (TextView) findViewById(R.id.tvCred);
tvQuestion = (TextView) findViewById(R.id.tvQuestion);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rg = (RadioGroup) findViewById(R.id.rg);
btNext = (Button) findViewById(R.id.btNext);
tvQuestion.setText(questions[flag]);
tvCred.setText(TotalCoins);
btNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(answer[flag]))
{
correct++;
coins++;
}
else
{
wrong++;
}
flag++;
if (flag < questions.length)
{
tvQuestion.setText(questions[flag]);
}
else
{
score = correct;
Intent in = new Intent(getApplicationContext(), Results.class);
in.putExtra("passedCredits",credit);
in.putExtra("passedCorrect",correct);
in.putExtra("passedWrong",wrong);
in.putExtra("passedCoins",coins);
in.putExtra("passedScore",score);
startActivity(in);
}
}
});
}
}
答案 0 :(得分:0)
nathan Tuggy给出的HashMap Link实际上是实现你想要的好方法,但我认为你不熟悉这种语言..
你如何使用类似的排序机制在应用程序的每个开始时更改问题的队列位置,这个想法是这样的..(记住不只是复制粘贴此代码,根据需要更改和添加变量)
for (int i = 0; i < 10; i++) {
String Qtemp;
String Atemp;
int switch;
Random c = new Random();
Random d = new Random();
flag= c.nextInt(question.length);
switch = d.nextInt(question.length);
Qtemp= question[c]
Atemp = answer[c]
question[c] = question[switch];
answer[c] = answer[switch];
question[switch] = Qtemp;
answer[switch] = Atemp; }
这个想法是这样的,只是尝试在一个函数中创建并调用它,这样你的问题就会在应用程序的开头洗牌。
我真的不推荐这种方式,因为它不是正确的方式,但在你的情况下,我只是想告诉你任何&#39;让问题改组的方法。