Android - 循环随机数组并且不重复

标签 android

我是 Android 应用程序开发的初学者。下面的代码是一个测验应用程序,我希望它循环随机问题并且不重复问题,我尝试使用 flag2 随机生成问题,但出现编译错误,任何人都可以帮我解决这个问题。我也是 Java 初学者。

TextView tv;
Button btn1;
RadioButton rb1,rb2,rb3;
RadioGroup rg;

String Questions[]={"What is 1+1?","Capital of USA?","What is 2+2","Echo with Laughter","Warg"};
String opt[]={"2","3","4", "New York","Washington DC","Maryland", "5","4","6","Stairway to Heaven","Hotel California","Highway to hell","Jon","Bran","Dario" };
String ans[]={"2","Washington DC","4","Stairway to heaven","Bran"};



int flag=0;
public static int correct;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    tv=(TextView)findViewById(R.id.textView2);
    btn1=(Button)findViewById(R.id.button2);
    rg=(RadioGroup)findViewById(R.id.radioGroup);
    rb1=(RadioButton)findViewById(R.id.radioButton);
    rb2=(RadioButton)findViewById(R.id.radioButton2);
    rb3=(RadioButton)findViewById(R.id.radioButton3);

    tv.setText(Questions[flag]);
    rb1.setText(opt[0]);
    rb2.setText(opt[1]);
    rb3.setText(opt[2]);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
            String ansText = uans.getText().toString();

            if (ansText.equalsIgnoreCase(ans[flag])) {
                correct++;
            }
            else {
                Intent in = new Intent(getApplicationContext(),Token.class);
                startActivity(in);
            }
            flag++;
            if (flag < Questions.length) {
                tv.setText(Questions[flag]);
                rb1.setText(opt[flag * 3]);
                rb2.setText(opt[(flag * 3)+1]);
                rb3.setText(opt[(flag * 3)+2]);
            }
            else {

                Intent in = new Intent(getApplicationContext(),Token.class);
                startActivity(in);
            }

        }

最佳答案

使用java.lang.Math.random()。 它将返回从 0.0 到 0.1 的值,将这些值转换为整数并得到整数问题

关于Android - 循环随机数组并且不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35716636/

相关文章:

android - 通过 DialogFragment android 将 TimePickerDialog 的结果设置为按钮

java - 致命异常 : main java. lang.NoClassDefFoundError : external library. jar.class Android studio

java - 安卓 native : cannot allocate large memory under native layer

android - 如何从 ListView 的右边缘设置垂直滚动条的边距?

java - 打开失败 : EACCES (Permission denied)

android - 分页库过滤器/搜索

android - 更改拨号屏幕

javascript - 如何将后台作业转换为函数之类的东西?

android - 如何在 Android 中使用 Union

java - 在json对象android中获取数组中的值