【问题标题】:List Array Shuffle or bang my head against the wall?List Array Shuffle 还是把我的头撞到墙上?
【发布时间】:2012-06-09 00:17:42
【问题描述】:

我正在尝试将 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(""));
                }
            }
    });
}

【问题讨论】:

  • 您的按钮逻辑有问题吗?洗牌?你试过什么?
  • 请向我们展示您的代码。 SO 的精神是帮助人们解决自己的问题(并学会这样做)......而不是为(简单的)问题提供固定的解决方案。
  • 您绝对应该在此处发布您的一些尝试。我想你会发现,如果人们看到你先给它“旧的大学尝试”,他们会更倾向于提供帮助。
  • 呃...我们应该如何帮助您?
  • 这不是很清楚。如果你只是想随机排列一个数组,谷歌就是:随机数组排列。两行代码。

标签: java android arrays list


【解决方案1】:

洗牌很容易:

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++ 向量

【讨论】:

  • Collin 我已经尝试过尽可能多的形式,因为我是 Java 新手。它警告我整数。我的记忆已经从我的头撞到墙上了。
  • 就整数向您发出警告?警告是什么?我没有编译这个,所以我可能搞砸了。
  • 这是我第一次看到这个。谢谢,我会试试的。我相信这会奏效
  • 你是对的,.at 给出了警告。再次感谢。我检查了你的答案。
  • 为什么我的问题是 -1 是不是很糟糕?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多