我可以將我設置爲android中每個按鈕的字符串隨機化,但有相同字母的重複。我怎麼做沒有任何重複的信件?如何隨機化一個沒有任何重複/重複字符串的數組?
Random random = new Random();
String[] letters = {"G","O","K","U","H","A","N","L","Z","M"};
c1r1 = (Button) findViewById(R.id.btn1);
c2r1 = (Button) findViewById(R.id.btn2);
c3r1 = (Button) findViewById(R.id.btn3);
c4r1 = (Button) findViewById(R.id.btn4);
c5r1 = (Button) findViewById(R.id.btn5);
c1r2 = (Button) findViewById(R.id.btn6);
c2r2 = (Button) findViewById(R.id.btn7);
c3r2 = (Button) findViewById(R.id.btn8);
c4r2 = (Button) findViewById(R.id.btn9);
c5r2 = (Button) findViewById(R.id.btn10);
first = (Button) findViewById(R.id.first);
second = (Button) findViewById(R.id.second);
third = (Button) findViewById(R.id.third);
fourth = (Button) findViewById(R.id.fourth);
c1r1.setText("" + letters[random.nextInt(letters.length)]);
c2r1.setText("" + letters[random.nextInt(letters.length)]);
c3r1.setText("" + letters[random.nextInt(letters.length)]);
c4r1.setText("" + letters[random.nextInt(letters.length)]);
c5r1.setText("" + letters[random.nextInt(letters.length)]);
c1r2.setText("" + letters[random.nextInt(letters.length)]);
c2r2.setText("" + letters[random.nextInt(letters.length)]);
c3r2.setText("" + letters[random.nextInt(letters.length)]);
c4r2.setText("" + letters[random.nextInt(letters.length)]);
c5r2.setText("" + letters[random.nextInt(letters.length)]);
呃,等等。你使用我的解決方案並清理了它? :) –
謝謝@EmanuelSeibold和Abhishek Aryan。它完美的工作:) –