-1

我正在尝试创建一个测验,其中我有 10 个问题(课程)随机化,但我只想显示 5 个问题(课程),然后分数将在测验结束时显示.. 我该怎么做?请.....或者是否可以限制android中的随机化?非常感谢你的帮忙..

这是代码,如果我开始播放,则必须进行随机化:

public class Whoami extends Activity {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_whoami);

    Button bplay =(Button) findViewById(R.id.bplay);
     bplay.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View arg0) {


            Random random = new Random();               
            random.nextInt(10);

                for (int i = 0; i < 5; i++);

                {

                    Intent newActivity1 = new Intent(Whoami.this,Question1.class);
                    startActivity(newActivity1);
                    finish();

                }
                {
                    Intent newActivity2 = new Intent(Whoami.this,Question2.class);     
                    startActivity(newActivity2);
                    finish();
                }
                {
                    Intent newActivity3 = new Intent(Whoami.this,Question3.class);     
                    startActivity(newActivity3);
                    finish();
                }
                {
                     Intent newActivity4 = new Intent(Whoami.this,Question4.class);     
                     startActivity(newActivity4);
                     finish();
                }
                {
                    Intent newActivity5 = new Intent(Whoami.this,Question5.class);     
                    startActivity(newActivity5);
                    finish();
                }
                {
                    Intent newActivity6 = new Intent(Whoami.this,Question6.class);     
                    startActivity(newActivity6);
                    finish();
                }
                {
                    Intent newActivity7 = new Intent(Whoami.this,Question7.class);     
                    startActivity(newActivity7);
                    finish();
                }
                {
                     Intent newActivity8 = new Intent(Whoami.this,Question8.class);     
                    startActivity(newActivity8);
                   finish();
                }
                {
                    Intent newActivity9 = new Intent(Whoami.this,Question9.class);     
                    startActivity(newActivity9);
                    finish();
                }
                {
                    Intent newActivity10 = new Intent(Whoami.this,Question10.class);     
                    startActivity(newActivity10);
                    finish();
                }
                Intent i = new Intent (Whoami.this, Score.class);
                startActivity(i);
                finish();
            }
     });
};

}

4

1 回答 1

1

It is never a good idea to hardcode content. I assume the questions only differ in terms of their wording. You should create one class for the question that is passed an integer indicating the question index (between 1 and 10) in its constructor.

Staying with your current class "system", you could do something like this inside the onClick method:

Random random = new Random();               
int questions = 10;
boolean used[] = new boolean[questions];
for (int i = 0; i < questions; i++) used[i] = false;

for (int i = 0; i < 5; i++) {
    int q;
    do { q = random.nextInt(questions); }
    while (used[q]);
    used[q] = true;

    Intent i = null;

    switch (q) {
    case 0: intent = new Intent(Whoami.this,Question1.class); break;
    case 1: intent = new Intent(Whoami.this,Question2.class); break;
    case 2: intent = new Intent(Whoami.this,Question3.class); break;
    case 3: intent = new Intent(Whoami.this,Question4.class); break;
    case 4: intent = new Intent(Whoami.this,Question5.class); break;
    case 5: intent = new Intent(Whoami.this,Question6.class); break;
    case 6: intent = new Intent(Whoami.this,Question7.class); break;
    case 7: intent = new Intent(Whoami.this,Question8.class); break;
    case 8: intent = new Intent(Whoami.this,Question9.class); break;
    case 9: intent = new Intent(Whoami.this,Question10.class); break;
    }

    startActivity(intent);
}

Intent i = new Intent (Whoami.this, Score.class);
startActivity(i);
finish();

Also, I would consider it a bad idea to start all the activities at once. Rather start them when the previous one has finished.

于 2013-02-17T01:16:32.110 回答