gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:37:20 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com