1

我正在尝试将数字列表从 1-15 随机排列。然后取前 5 个数字并将索引 1 分配给第一个 btn 的文本 第二个数字的文本分配给第二个 btn 等的文本......问题是我几乎尝试了我在 Stackoverflow 中读到的所有内容,但没有任何帮助. 请帮忙。

    public class MainActivity extends Activity implements  OnClickListener,  
CountdownTimerFinishedListener{
/** Called when the activity is first created. */
//SET UP SPINS

 private TextView spins;    
 private Button spin;
 private Button s_1;
 private Button s_2;
 private Button s_3;
 private Button s_4;
 private Button s_5;

  private static final Random rand = new Random();

int totalspins = 15;
int gamescore = 0;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
int[] b_list ={1,2,3,4,5,6,7,8,9,10,12,13,14,15};

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.play_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    gamescore=(0);
    score = (TextView) findViewById(R.id.score);
    score.setText(String.valueOf("SCORE: "+ gamescore));
    totalspins = (15);
    spins = (TextView) findViewById(R.id.spins);
    spins.setText(String.valueOf("SPINS: "+ totalspins));
    spin = (Button) findViewById(R.id.spin);
    s_1 = (Button) findViewById(R.id.spin_1);
    s_2 = (Button) findViewById(R.id.spin_2);
    s_3 = (Button) findViewById(R.id.spin_3);
    s_4 = (Button) findViewById(R.id.spin_4);
    s_5 = (Button) findViewById(R.id.spin_5);
    b_1 = (Button) findViewById(R.id.btn_1);
    b_2 = (Button) findViewById(R.id.btn_2);
    b_3 = (Button) findViewById(R.id.btn_3);
    b_4 = (Button) findViewById(R.id.btn_4);
    b_5 = (Button) findViewById(R.id.btn_5);

    //int randIntb1 = rand.nextInt(15)+1;
     b_1.setText(String.valueOf(b_list[0]));//randIntb1
    //int randIntb2 = rand.nextInt(15)+1;
    b_2.setText(String.valueOf(b_list[1]));//randIntb2
    //int randIntb3 = rand.nextInt(15)+1;
    b_3.setText(String.valueOf(b_list[2]));//randIntb3
    //int randIntb4 = rand.nextInt(15)+1;
    b_4.setText(String.valueOf(b_list[3]));//randIntb4
    //int randIntb5 = rand.nextInt(15)+1;
    b_5.setText(String.valueOf(b_list[4]));//randIntb5

     spin.setOnClickListener(new OnClickListener() {
        private Object Button;
        public void onClick(View v) {
            //spin.setEnabled(false);
            int randInts1 = rand.nextInt(17)+1;
            s_1.setText(String.valueOf(randInts1));
            s1=(randInts1);
            int randInts2 = rand.nextInt(17)+15;
            s_2.setText(String.valueOf(randInts2));
            s2=(randInts2);
            int randInts3 = rand.nextInt(17)+30;
            s_3.setText(String.valueOf(randInts3));
            s3=(randInts3);
            int randInts4 = rand.nextInt(17)+45;
            s_4.setText(String.valueOf(randInts4));
            s4=(randInts4);
            int randInts5 = rand.nextInt(17)+60;
            s_5.setText(String.valueOf(randInts5));
            s5=(randInts5);


            totalspins = (totalspins - 1);
            spins.setText(String.valueOf("SPINS: "+ totalspins));
            if(totalspins <= 0){
                totalspins =(15);
            }
            }

    });






    b_1.setOnClickListener(new OnClickListener() {
        private Object Button;
        public void onClick(View v){
            if (b_1.getText() == (s_1.getText())){
                gamescore =(gamescore + s1);
                score.setText(String.valueOf("SCORE: "+ gamescore));
                b_1.setText(String.valueOf(""));
                s_1.setText(String.valueOf(""));
                }
            }
    });
    b_2.setOnClickListener(new OnClickListener() {
        private Object Button;
        public void onClick(View v){
            if (b_2.getText() == (s_1.getText())){
                gamescore =(gamescore + s1);
                score.setText(String.valueOf("SCORE: "+ gamescore));
                b_2.setText(String.valueOf(""));
                s_1.setText(String.valueOf(""));
                }
            }
    });
    b_3.setOnClickListener(new OnClickListener() {
        private Object Button;
        public void onClick(View v){
            if (b_3.getText() == (s_1.getText())){
                gamescore =(gamescore + s1);
                score.setText(String.valueOf("SCORE: "+ gamescore));
                b_3.setText(String.valueOf(""));
                s_1.setText(String.valueOf(""));
                }
            }
    });
}
4

1 回答 1

6

洗牌很容易:

ArrayList<Integer> numbers = new ArrayList<Integer>()
for(int i = 1; i <= 15; i++) {
    numbers.add(i);
}

Collections.shuffle(numbers);

然后,当您想让它们成为按钮的文本时,只需执行以下操作:

b_1.setText(numbers.get(0).toString());
b_2.setText(numbers.get(1).toString());
b_3.setText(numbers.get(2).toString());
b_4.setText(numbers.get(3).toString());
b_5.setText(numbers.get(4).toString());

但是,将这些按钮保留在某种数据结构中可能会更好:

ArrayList<Button> buttons = new ArrayList<Button>();
//Fill list to your heart's content

Collections.shuffle(numbers);
for(int i = 0; i < buttons.length() && i < numbers.length(); i++) {
    buttons.get(i).setText(numbers.get(i).toString());
}

为我编辑忘记了 Java 列表使用 get() 而不是 at() 像 C++ 向量

于 2012-06-09T00:26:07.653 回答