i have a problem in randomizing values. what i want to do is :
i have 4 editFields and when i type in all fields and click the button "shuffle players" it should show all the players from editFields down in viewText below randomly and non-repeated.
this my code for onlick function:
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);
}
but the problem i keep getting the same random name for all Textviews
and here is an image showing the logic error: ![enter image description here][1]
please help me and show me my mistakes thanks
Try this solution works like a charm
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));
}
You only generate one random number, so the index is always the same and you only get one name in all your TextViews. You could shuffle your array like explained here and get all the names from it and bind them to your TextViews.
您为所有视图设置相同的随机值意味着您选择随机一次然后分配它您应该做类似的事情但在每次分配之前再次调用随机
Change
int index = new Random().nextInt(players.length);
String random = (players[index]);
View1.setText(random);
View2.setText(random);
View3.setText(random);
View4.setText(random);
to
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);
This way you get different numbers but there is no guarantee that there will be no repitition
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.