2016-01-22 71 views
-4

在我的java代码中,我随机选择一个数字并从数组中获取数字所赋值的值/字符串。这工作正常,但它已被提取后,我想要将数字存储在列表或数组中,然后当再次生成数字时,我可以检查该数字是否存在于数组或列表中,以及它是否会选择另一个数字,如果不匹配,则显示array.xml中该数字的值/字符串。这怎么能实现?我的代码是否正确实现?检查你的列表/数组中是否存在随机数

array.xml

<array name="OneTimesTables"> 
    <item>1 x 1 = </item> 
    <item>1 x 2 = </item> 
    <item>1 x 3 = </item> 
    <item>1 x 4 = </item> 
    <item>1 x 5 = </item> 
    <item>1 x 6 = </item> 
    <item>1 x 7 = </item> 
    <item>1 x 8 = </item> 
    <item>1 x 9 = </item> 
    <item>1 x 10 = </item> 
    <item>1 x 11 = </item> 
    <item>1 x 12 = </item> 
</array> 

我的Java代码

public class Test extends AppCompatActivity { 

String[] MyArray; 
TextView Questions; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test); 

    final List<String> PastQuestions = new ArrayList<String>(); 

    MyArray = getResources().getStringArray(R.array.OneTimesTables); 
    Questions = (TextView)findViewById(R.id.RandomTestActivity_textView); 
    Button button = (Button)findViewById(R.id.RandomTestActivity_button); 

    int count = (new Random()).nextInt(12); 
    Questions.setText(MyArray[count]); 
    PastQuestions.add(MyArray[count]); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int count = (new Random()).nextInt(12); 
      if (MyArray[count].equals(PastQuestions)) { 
       int count2 = (new Random()).nextInt(12); 
       Questions.setText(MyArray[count2]); 
       PastQuestions.add(MyArray[count2]); 
      } else { 
       Questions.setText(MyArray[count]); 
       PastQuestions.add(MyArray[count]); 
      } 
     } 
    }); 
    RandomiseArray(); 
} 

private void RandomiseArray() { 
    ArrayList<String> list = new ArrayList<>(); 

    for(String item: MyArray){ 
     list.add(item); 
    } 
    Collections.shuffle(list); 
} 
} 
+1

了代码正确实现这一点? – Ceelos

+0

不,因为它会一遍又一遍地打印相同的数字! – RexDough

回答

1

使用Set,例如一个HashSet<Integer>。一套提供快速检查,看看值是否已经存在(contains())。

Random rnd = new Random(); 

// Generate 5 unique numbers 0-9 
Set<Integer> used = new HashSet<>(); 
for (int i = 0; i < 5; i++) { 
    int number = rnd.nextInt(10); 
    while (used.contains(number)) { 
     System.out.println("Skipping duplicate: " + number); 
     number = rnd.nextInt(10); 
    } 
    used.add(number); 
    System.out.println(number); 
} 

输出

0 
2 
7 
6 
Skipping duplicate: 2 
4 

现在,如果你将被耗尽可用号码列表中,重试while环路将开始旋转,随机搜索未使用的号码。

如果您知道您将使用全部或大部分可用数字,最好将所有数字添加到列表中并随机播放,然后从列表中提取值。

// Generate random list of 10 unique numbers 0-9 
List<Integer> list = new ArrayList<>(); 
for (int i = 0; i < 10; i++) 
    list.add(i); 
Collections.shuffle(list); 
System.out.println(list); 

输出

[6, 2, 3, 5, 8, 0, 9, 1, 7, 4] 
0

我认为这个问题会发生在这里。

int count = (new Random()).nextInt(12); 
if (MyArray[count].equals(PastQuestions)) { 
    int count2 = (new Random()).nextInt(12); 
    Questions.setText(MyArray[count2]); 
    PastQuestions.add(MyArray[count2]); 
} else { 
    Questions.setText(MyArray[count]); 
    PastQuestions.add(MyArray[count]); 
} 

如果你想确保该项目是与你在PastQuestions挑出任何项目不同,你需要在PastQuestions每个元素比较MyArray[count]。同时,如果您选择MyArray[count].equals(PastQuestions),您可能会随机选择一个数字,也许它可能会再次通过项目,所以您需要重新设计此部分以考虑这种情况。

一种解决方案可能满足您的要求:

  1. itemArray店内所有物品;
  2. count store你选择了多少物品; count = 0初始
  3. 当点击按钮时,你随机得到一个数字:pick =(new Random())。nextInt(12-count);
  4. 添加item itemArray [pick]到你的PastQuestions。
  5. 从itemArray

删除此项目,

public class Test extends AppCompatActivity { 

String[] MyArray; 
TextView Questions; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test); 

    final List<String> PastQuestions = new ArrayList<String>(); 

    MyArray = getResources().getStringArray(R.array.OneTimesTables); 
    Questions = (TextView)findViewById(R.id.RandomTestActivity_textView); 
    Button button = (Button)findViewById(R.id.RandomTestActivity_button); 

    //The current length of PastQuestions 
    int length = 0 

    int count = (new Random()).nextInt(12); 
    Questions.setText(MyArray[count]); 
    PastQuestions.add(MyArray[count]); 
    length++; 
    MyArray.remove(count); 

    //in order to access the length, you need a final variable 
    final int[] lengthAccessInListenr = {length}; 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      int count = (new Random()).nextInt(12-lengthAccessInListenr[0]); 
      Questions.setText(MyArray[count]); 
      PastQuestions.add(MyArray[count]); 
      lengthAccessInListenr[0]++; 
      MyArray.remove(count); 
     } 
    }); 
    RandomiseArray(); 
} 

private void RandomiseArray() { 
    ArrayList<String> list = new ArrayList<>(); 

    for(String item: MyArray){ 
     list.add(item); 
    } 
    Collections.shuffle(list); 
} 
}