如何不重复就可以随机回答我的问题?

时间:2018-10-03 20:46:29

标签: java android eclipse

打包com.quizrandom;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Random;

导入android.app.Activity;

导入android.content.Intent;

导入android.os.Bundle;

导入android.view.View;

import android.widget.Button;

导入android.widget.TextView;

导入android.widget.Toast;

公共类quiz1扩展了Activity实现的View.OnClickListener {

Button btn_one, btn_two, btn_three, btn_four;
TextView tv_question;
 int score,x1;
 int e1;
private Question question = new Question();
private int questionLength = question.questions.length;
private String answer;
Random random;
int[] solutionArray = {}; 
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

   random = new Random();


    btn_one = (Button)findViewById(R.id.btn_one);
    btn_one.setOnClickListener( this);
    btn_two = (Button)findViewById(R.id.btn_two); 
    btn_two.setOnClickListener( this);
    btn_three = (Button)findViewById(R.id.btn_three);
    btn_three.setOnClickListener(this);
    btn_four = (Button)findViewById(R.id.btn_four);
    btn_four.setOnClickListener(this);

    tv_question = (TextView)findViewById(R.id.tv_question);







    NextQuestion(random.nextInt(questionLength));



}

public void go1 (View v)
{
    Intent i = new Intent (this,MainActivity.class);
    startActivity(i);
}

@Override
public void onClick(View v) {
     ArrayList<Integer> arrayB = new ArrayList<Integer>();
        for (int i = 1; i <= 10; ++i) arrayB.add(i);
        Collections.shuffle(arrayB);


    switch (v.getId()){
    case R.id.btn_one:
        if(btn_one.getText() == answer){
            score ++;
            Toast.makeText(quiz1.this, "You Are Correct" +score, Toast.LENGTH_SHORT).show();

            NextQuestion(random.nextInt(questionLength));




        }
        else{

            NextQuestion(random.nextInt(questionLength));

            Toast.makeText(quiz1.this, "You Are Not Correct" +score, Toast.LENGTH_SHORT).show();

        }

        break;

    case R.id.btn_two:
        if(btn_two.getText() == answer){
                        score ++;
            Toast.makeText(quiz1.this, "You Are Correct" +score, Toast.LENGTH_SHORT).show();
            NextQuestion(random.nextInt(questionLength));

        }else{
            NextQuestion(random.nextInt(questionLength));

            Toast.makeText(quiz1.this, "You Are Not Correct" +score, Toast.LENGTH_SHORT).show();
        }

        break;

    case R.id.btn_three:
        if(btn_three.getText() == answer){

            score ++;
            Toast.makeText(quiz1.this, "You Are Correct" +score, Toast.LENGTH_SHORT).show();
            NextQuestion(random.nextInt(questionLength));

        }else{
            NextQuestion(random.nextInt(questionLength));

            Toast.makeText(quiz1.this, "You Are Not Correct" +score, Toast.LENGTH_SHORT).show();
        }

        break;

    case R.id.btn_four:
        if(btn_four.getText() == answer){

            score ++;
            Toast.makeText(quiz1.this, "You Are Correct" +score, Toast.LENGTH_SHORT).show();
            NextQuestion(random.nextInt(questionLength));


        }else{

            Toast.makeText(quiz1.this, "You Are Not Correct" +score, Toast.LENGTH_SHORT).show();
            NextQuestion(random.nextInt(questionLength));

        }

        break;
}

}


static void shuffleArray(int[] ar)
  {
    // If running on Java 6 or older, use `new Random()` on RHS here
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
      int index = rnd.nextInt(i + 1);
      // Simple swap
      int a = ar[index];
      ar[index] = ar[i];
      ar[i] = a;
    }
  }

    private void NextQuestion (int num){
        tv_question.setText(question.getQuestion(num));
        btn_one.setText(question.getchoice1(num));
        btn_two.setText(question.getchoice2(num));
        btn_three.setText(question.getchoice3(num));
        btn_four.setText(question.getchoice4(num));

        answer = question.getCorrectAnswer(num);
    }}

Question.java

打包com.quizrandom;

公共课问题{

public String questions[] = {
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15"
};

public String choices[][] = {
        {"HTML", "CSS", "Vala", "PHP"},
        {"Punction Marks", "Back-Slash", "Brackets", "Semi Colon"},
        {"Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"},
        {"bla1", "bla2", "bla3", "PHP4"}
};

public String correctAnswer[] = {
    "PHP",
    "Brackets",
    "Commercial Applications",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1",
    "bla1"
};

public String getQuestion(int a){
    String question = questions[a];
    return question;
}

public String getchoice1(int a){
    String choice = choices[a][0];
    return choice;
}

public String getchoice2(int a){
    String choice = choices[a][1];
    return choice;
}

public String getchoice3(int a){
    String choice = choices[a][2];
    return choice;
}

public String getchoice4(int a){
    String choice = choices[a][3];
    return choice;
}

public String getCorrectAnswer(int a){
    String answer = correctAnswer[a];
    return answer;
}}

0 个答案:

没有答案
相关问题