随机数测验应用程序

时间:2016-03-09 12:31:58

标签: java android

这是我的主要活动,我不知道我应该在哪里放置问题的代码来为我的应用程序测验生成随机问题。这个概念是为用户生成随机问题。

qid是包含问题的字符串

      package com.example.triviality;
    import java.util.List;
    import java.util.Random;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;

    public class QuizActivity extends Activity {
        List<Question> quesList;
        int score=0;
        int qid=0;
        Question currentQ;
        TextView txtQuestion;
        RadioButton rda, rdb, rdc;
        Button butNext;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quiz);
            DbHelper db=new DbHelper(this);
            quesList=db.getAllQuestions();
            currentQ=quesList.get(qid);
            txtQuestion=(TextView)findViewById(R.id.textView1);
            rda=(RadioButton)findViewById(R.id.radio0);
            rdb=(RadioButton)findViewById(R.id.radio1);
            rdc=(RadioButton)findViewById(R.id.radio2);
            butNext=(Button)findViewById(R.id.button1);
            setQuestionView();
            butNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                    RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
                    Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                    if(currentQ.getANSWER().equals(answer.getText()))
                    {
                        score++;
                        Log.d("score", "Your score"+score);
                    }
                    if(qid<8){
                        currentQ=quesList.get(qid);
                        setQuestionView();
                    }else{
                        Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                        Bundle b = new Bundle();
                        b.putInt("score", score); //Your score
                        intent.putExtras(b); //Put your score to your next Intent
                        startActivity(intent);
                        finish();
                    }
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_quiz, menu);
            return true;
        }
        private void setQuestionView()
        {
            txtQuestion.setText(currentQ.getQUESTION());
            rda.setText(currentQ.getOPTA());
            rdb.setText(currentQ.getOPTB());
            rdc.setText(currentQ.getOPTC());
            qid++;
        }
    }

I dont know where to put the code to random the qid

heres the code for my Database that contains the questions for the quiz

package com.example.triviality;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "triviaQuiz";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; //correct option
    private static final String KEY_OPTA= "opta"; //option a
    private static final String KEY_OPTB= "optb"; //option b
    private static final String KEY_OPTC= "optc"; //option c
    private SQLiteDatabase dbase;
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase=db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
                +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
        db.execSQL(sql);
        addQuestions();
        //db.close();
    }
    private void addQuestions()
    {
        Question q1=new Question("The total number of items representable by a 4 byte binary word (using conventionalabbreviations) is?","16G", "32M", "4G", "4G");
        this.addQuestion(q1);
        Question q2=new Question("Using two's complement encoding, subtract 11100 from 01101, and report the result as a5-bit two's complement binary number.", "101001", "01001", "10001", "10001");
        this.addQuestion(q2);
        Question q3=new Question("Convert the 8-bit two's complement number 10001111 into decimal.","-241", "-143","-113", "-113" );
        this.addQuestion(q3);
        Question q4=new Question("what do you know about JP?", "JP is programmer home", "JP also realigy home", "all answer is true","all answer is true");
        this.addQuestion(q4);
        Question q5=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q5);
        Question q6=new Question("Assembly Language instruction sets can be categorized into three basic types ofinstruction:","Direct, Register, and Indirect","High Level, Assembly, and Machine","Operations, Data Movement and Control","Operations, Data Movement and Control");
        this.addQuestion(q6);
        Question q7=new Question("A label in assembly language code is:","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q7);
        Question q8=new Question("what do you learn in JP?","An abbreviation for an instruction","PAn adhesive sticker placed on the front page of the code","A symbolic representation of a memory location","A symbolic representation of a memory location");
        this.addQuestion(q8);
        Question q9=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q9);
        Question q10=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q10);
        Question q11=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q11);
        Question q12=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q12);
        Question q13=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q13);
        Question q14=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q14);
        Question q15=new Question("what do you learn in JP?","Realigy","Programming","all answer is true","all answer is true");
        this.addQuestion(q15);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        // Create tables again
        onCreate(db);
    }
    // Adding new question
    public void addQuestion(Question quest) {
        //SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION());
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        // Inserting Row
        dbase.insert(TABLE_QUEST, null, values);
    }
    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        dbase=this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }
        // return quest list
        return quesList;
    }
    public int rowcount()
    {
        int row=0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row=cursor.getCount();
        return row;
    }
}

2 个答案:

答案 0 :(得分:0)

"SELECT * FROM" + TABLE_QUEST + "ORDER BY RANDOM()";

答案 1 :(得分:0)

到目前为止我所了解的内容:

你想从你的quesList中挑选一个随机问题而不知道该怎么做 - 对吧?确定..

Java解决方案:

您必须引入java.util.Random成员变量,因为每次要获取随机数时,实例化新的Random实例并不是一个好主意。

在您的代码中

currentQ=quesList.get(qid);

您从该列表中选择了qid个问题,但您想选择一个随机问题。

使用java.util.Random#nextInt(int n),其中随机数将从[0,n)中选取 - 意味着n不属于该集合。因此questList.size()n的热门候选人。

之后,最好从questList中删除已挑选的问题,因为您不想两次选择问题,是吗?

数据库解决方案:

更有效的解决方案是从数据库中随机选择8个问题。

如果您使用SqlLite数据库,查询将查看@rva答案。添加限制表达式将使此解决方案执行得更好。

但是,您没有告诉我们您的数据库的任何信息。这是我不能多说一个数据库解决方案的方式。