指定した配列内のランダム9ビット数を取得
3863 ワード
, 。
X ,
java android
配列内の重複しない数字を取得し、中間スペースを構成する9桁の文字列をランダムに取得して表示結果:2369 0457
方法1
//
private int[] array = new int[]{0, 2, 3, 4, 5, 6, 7, 8, 9};
private void testArray() {
TextView textView = findViewById(R.id.textView);
String str = "";
for (int i = 0; i < array.length; i++){
int index = new Random().nextInt(array.length);
if (str.contains(array[index] + "")){
i--;
}else{
if (i == 4){
str = str + " ";
}else {
str = str + array[index];
}
}
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
方法2
public static String getRandomString() {
String ret = "";
int[] result = new int[8];
int cnt = 0;
while (cnt < 8) {
int num = (int) (Math.random() * (10 - 0));
boolean flag = true;
for (int j = 0; j < 8; j++) {
if (num == result[j]) {
flag = false;
break;
}
}
if (flag && num != 1) {
result[cnt] = num;
ret += result[cnt];
cnt++;
}
}
return ret;
}
private void SetRandom(String randomStr) {
String random = randomStr;
String modRandom = "";
for (int i = 0; i < 4; i++) {
modRandom += random.charAt(i);
}
modRandom += " ";
for (int j = 4; j < 8; j++) {
modRandom += random.charAt(j);
}
Toast.makeText(this, modRandom, Toast.LENGTH_SHORT).show();
}
:SetRandom(getRandomString());
ランダムに8桁の数字を生成し、音声を再生します.
/**
* raw mp3
* MediaPlayer mediaPlayer = MediaPlayer.create(this, mediaArry[0]);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
}
});
*/
int[] mediaArry = new int[]{R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify,
R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify, R.raw.order_new_notify};
List mediaPlayerList = new ArrayList();
private void openRawMusicS() {
mediaPlayerList.get(0).start();
for (int i = 0; i < mediaPlayerList.size() - 1; i++){
final int finalI = i;
mediaPlayerList.get(i).setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayerList.get(finalI + 1).start();
}
});
}
}
private int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
private void testArray() {
String str = "";
for (int i = 0; i < 9; i++){
int index = new Random().nextInt(array.length);
if (str.contains(array[index] + "")){
i--;
}else{
if (i == 4){
str = str + " ";
}else {
str = str + array[index];
mediaPlayerList.add(MediaPlayer.create(this, mediaArry[index]));
}
}
}
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}