如何进行非重复随机数组选择(android)?

时间:2014-05-12 08:59:42

标签: android random

我在随机化值方面存在问题。 我想做的是:

我有4个editFields,当我输入所有字段并点击按钮"随机播放器" 它应该随机且不重复地在viewText中向下显示editFields中的所有玩家。

这是我的onlick功能代码:

 public void RandomPlayers(View v1){


    EditText text1 = (EditText)findViewById(R.id.editText1);
    TextView View1 = (TextView)findViewById(R.id.player1);
    EditText text2 = (EditText)findViewById(R.id.editText2);
    TextView View2 = (TextView)findViewById(R.id.player2);
    EditText text3 = (EditText)findViewById(R.id.editText3);
    TextView View3 = (TextView)findViewById(R.id.player3);
    EditText text4 = (EditText)findViewById(R.id.editText4);
    TextView View4 = (TextView)findViewById(R.id.player4);

    String value1 = text1.getText().toString();
    String value2 = text2.getText().toString();
    String value3 = text3.getText().toString();
    String value4 = text4.getText().toString();

    String[] players = {value1, value2, value3, value4};

    int index = new Random().nextInt(players.length);
    String random = (players[index]);
    View1.setText(random);
    View2.setText(random);
    View3.setText(random);
    View4.setText(random);

}

但问题是我一直为所有Textviews获得相同的随机名称

这是一个显示逻辑错误的图像: ![在此输入图像说明] [1]

请帮助我,告诉我我的错误 感谢

4 个答案:

答案 0 :(得分:3)

尝试此解决方案就像魅力一样

public void RandomPlayers(View v1) {

        List<String> values = new ArrayList<String>();

        EditText text1 = (EditText) findViewById(R.id.editText1);
        TextView View1 = (TextView) findViewById(R.id.player1);
        EditText text2 = (EditText) findViewById(R.id.editText2);
        TextView View2 = (TextView) findViewById(R.id.player2);
        EditText text3 = (EditText) findViewById(R.id.editText3);
        TextView View3 = (TextView) findViewById(R.id.player3);
        EditText text4 = (EditText) findViewById(R.id.editText4);
        TextView View4 = (TextView) findViewById(R.id.player4);

        values.add(text1.getText().toString());
        values.add(text2.getText().toString());
        values.add(text3.getText().toString());
        values.add(text4.getText().toString());

        Collections.shuffle(values);

        View1.setText(values.get(0));
        View2.setText(values.get(1));
        View3.setText(values.get(2));
        View4.setText(values.get(3));

    }

答案 1 :(得分:0)

您只生成一个随机数,因此索引始终相同,并且您只在所有TextView中获得一个名称。您可以像解释here那样对数组进行随机播放,并从中获取所有名称并将它们绑定到TextView。

答案 2 :(得分:0)

您为所有视图设置了相同的随机值 意思是你选择一次随机然后分配它 你应该做类似的事情,但在每次任务之前再次随机调用

答案 3 :(得分:0)

更改

int index = new Random().nextInt(players.length);
String random = (players[index]);
View1.setText(random);
View2.setText(random);
View3.setText(random);
View4.setText(random);

int index = new Random().nextInt(players.length);
String random = (players[index]);
View1.setText(random);

index = new Random().nextInt(players.length);
random = (players[index]);
View2.setText(random);

index = new Random().nextInt(players.length);
random = (players[index]);
View3.setText(random);

index = new Random().nextInt(players.length);
random = (players[index]);
View4.setText(random);

这样你会获得不同的数字,但无法保证不会有重复数据