如何让'再试一次'循环

时间:2015-05-23 16:24:56

标签: java android

当答案错误时,错误的答案将回滚再次尝试,直到答案正确为止。那么它应该跳到下一个问题。我的意思是,如果我们选择了错误的答案,它将会再次尝试,直到我们能够正确回答这个问题。

到目前为止,这是我的代码:

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);
}
}

1 个答案:

答案 0 :(得分:0)

如果我没有误会,只需改变&#34; flag ++&#34;的位置。

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);
    }
}