Java 如何制作';再试一次'-环
当答案错误时,错误答案将回滚以重试,直到答案正确为止。然后它应该跳转到下一个问题。我的意思是,如果我们选择了错误的答案,它将再次尝试,直到我们能够正确回答问题 以下是我目前的代码:Java 如何制作';再试一次'-环,java,android,Java,Android,当答案错误时,错误答案将回滚以重试,直到答案正确为止。然后它应该跳转到下一个问题。我的意思是,如果我们选择了错误的答案,它将再次尝试,直到我们能够正确回答问题 以下是我目前的代码: public class FamilyQuestion extends ActionBarActivity { TextView tv; Button btnNext; RadioGroup rg; RadioButton bt1,bt2,bt3; String Question[]={"HAHA","HAHA2"
public class FamilyQuestion extends ActionBarActivity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton bt1,bt2,bt3;
String Question[]={"HAHA","HAHA2","HAHA3"};
String ans[]={"i1","12","i3"};
String opt[]={"i1","lali","uu","12","hehe","oo","i3","jj","cc"};
int flag=0;
public static int mark,correct,wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit_question);
tv=(TextView) findViewById(R.id.tvque);
btnNext=(Button) findViewById(R.id.btnNext);
rg = (RadioGroup) findViewById(R.id.rg);
bt1 = (RadioButton) findViewById(R.id.btn1);
bt2 = (RadioButton) findViewById(R.id.btn2);
bt3 = (RadioButton) findViewById(R.id.btn3);
tv.setText(Question[flag]);
bt1.setText(opt[0]);
bt2.setText(opt[1]);
bt3.setText(opt[2]);
Toast.makeText(this, "Negative Mark : " + Test.tbflag, 1000).show();
btnNext.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
{
wrong++;
}
flag++;
if(flag<Question.length)
{
tv.setText(Question[flag]);
bt1.setText(opt[flag*3]);
bt2.setText(opt[(flag*3)+1]);
bt3.setText(opt[(flag*3)+2]);
}
else
{
if(Test.tbflag)
{
mark=correct-wrong;
}
else
{
mark=correct;
}
Intent in=new Intent(getApplicationContext(),ResultActivity.class);
in.putExtra("question", "family");
startActivity(in);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_family_question, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
公共类家庭问题扩展了ActionBarActivity{
文本视图电视;
按钮btnNext;
放射组rg;
单选按钮bt1、bt2、bt3;
字符串问题[]={“哈哈”,“哈哈2”,“哈哈3”};
字符串ans[]={“i1”、“12”、“i3”};
字符串opt[]={“i1”、“lali”、“uu”、“12”、“hehe”、“oo”、“i3”、“jj”、“cc”};
int标志=0;
公共静态整数标记,正确,错误;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u fruit\u question);
tv=(TextView)findviewbyd(R.id.tvque);
btnNext=(按钮)findViewById(R.id.btnNext);
rg=(放射组)findViewById(R.id.rg);
bt1=(单选按钮)findViewById(R.id.btn1);
bt2=(单选按钮)findViewById(R.id.btn2);
bt3=(单选按钮)findViewById(R.id.btn3);
tv.setText(问题[旗]);
bt1.setText(opt[0]);
bt2.setText(选项[1]);
bt3.setText(选项[2]);
Toast.makeText(这个“负标记:+Test.tbflag,1000).show();
btnNext.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
字符串ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
正确的++;
}
其他的
{
错误++;
}
flag++;
如果(flag如果我没有误解,请更改“flag++”的位置
public void onClick(视图v){
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
字符串ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
正确的++;
flag++;
}
其他的
{
错误++;
//根据您的环境,您可能需要将Toast.LENGTH\u SHORT更改为1000。
Toast.makeText(FamilyQuestion.this,“回答错误,请再试一次”,Toast.LENGTH_SHORT.show();
}
if(Toast.makeText处的flageror(这是“回答错误,请重试”,Toast.LENGTH_SHORT).show();据我所知,Toast.makeText()的参数“duration”必须是Toast.LENGTH\u SHORT或Toast.LENGTH\u LONG,不支持特定毫秒,如1000。我不知道为什么您可以使用1000,在我的项目中,这将是一个错误。因此,对于您的错误,只需将Toast.LENGTH\u SHORT替换为1000。当我更改为1000时,仍然是错误,@Kevin Feng原因无法解决makeText(此,“回答错误,请重试”,Toast.LENGTH_SHORT).show();很抱歉我回答错误,Toast.makeTest()中的参数“this”应该是“FamilyQuestion.this”。在内部类视图中。OnClickListener()中,“this”表示OnClickListener实例,但Toast.makeText()需要的是上下文或活动实例。我更改了答案。
public void onClick(View v) {
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText= uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
flag++;
}
else
{
wrong++;
// You may need to change Toast.LENGTH_SHORT to 1000 depending on your environment.
Toast.makeText(FamilyQuestion.this, "Wrong answer,please try again", Toast.LENGTH_SHORT).show();
}
if(flag<Question.length)
{
tv.setText(Question[flag]);
bt1.setText(opt[flag*3]);
bt2.setText(opt[(flag*3)+1]);
bt3.setText(opt[(flag*3)+2]);
}
else
{
if(Test.tbflag)
{
mark=correct-wrong;
}
else
{
mark=correct;
}
Intent in=new Intent(getApplicationContext(),ResultActivity.class);
in.putExtra("question", "family");
startActivity(in);
}
}