生成随机数而没有重复错误

时间:2013-12-05 15:04:45

标签: random dataadapter

我想生成范围(1-10)中的随机数,但没有重复的数字。这是我在Class DataAdapter中的一些代码..

public Cursor getTestData(int x)
{ 
 String tbSoalJawab = "";
 int y=0;

 switch(x)
 {
 case 11: tbSoalJawab = "level1_1"; break;
 case 12: tbSoalJawab = "level1_2"; break;
 case 13: tbSoalJawab = "level1_3"; break;
 case 1: y=1; break;
 case 2: y=1; break;
 case 3: y=1; break;
 }

 MyRandom randomSoal = new MyRandom();
 int idSoal = randomSoal.getRandom();

 try
 {
     String sql;
     if (y==1)
     {
         sql = "SELECT materi FROM materibilbul where id = '"+ x +"'";
     }
     else
     {
         sql = "SELECT soal,jawab FROM '"+ tbSoalJawab +"' where id = '"+ idSoal +"'";   
     }

     Cursor myCur = myDataBase.rawQuery(sql, null);
     if (myCur!=null)
     {
        myCur.moveToNext();
     }
     return myCur;
 }
 catch (SQLException mSQLException) 
 {
     Log.e(TAG, "getTestData >>"+ mSQLException.toString());
     throw mSQLException;
 }
}


 class MyRandom
 {
 Set<Integer> usedNums;

 public int getRandom()
 {   
     Random r = new Random();
     int rSoal = r.nextInt(10-1) + 1;

     while(usedNums.contains(rSoal))
     {
        rSoal = r.nextInt(10-1) + 1;
     }
     usedNums.add(rSoal);
     return rSoal;
 }

 public int reset()
 {
     usedNums.clear();
     return 0;
 }
 }

我有另一个类会调用此方法getTestData(1),但是我的应用程序被强制关闭...任何人都知道为什么?

0 个答案:

没有答案