Android 很难弄清楚如何在设定的次数内只生成两次设定值

Android 很难弄清楚如何在设定的次数内只生成两次设定值,android,button,random,Android,Button,Random,我将从这篇关于StackOverflow的文章开始,所以如果我遗漏了任何重要的内容,我很抱歉,我在编码和学习方面也是非常新的 我想做的是一个记忆游戏的形式,用户输入一个数字,它会生成那个数量的瓷砖,甚至只给他们设定值。我坚持给他们每个2个设定值,对于x数量的瓷砖,只有两个设定值 一个例子是,如果用户选择最小数量的tile,4,那么将有两个tile与A和两个tile与B。我现在的代码很混乱,但有点像草稿,它是在按钮上设置文本,而不是值。此代码也仅适用于4瓷砖示例 顺便说一句,我相信有一种更简单的方

我将从这篇关于StackOverflow的文章开始,所以如果我遗漏了任何重要的内容,我很抱歉,我在编码和学习方面也是非常新的

我想做的是一个记忆游戏的形式,用户输入一个数字,它会生成那个数量的瓷砖,甚至只给他们设定值。我坚持给他们每个2个设定值,对于x数量的瓷砖,只有两个设定值

一个例子是,如果用户选择最小数量的tile,4,那么将有两个tile与A和两个tile与B。我现在的代码很混乱,但有点像草稿,它是在按钮上设置文本,而不是值。此代码也仅适用于4瓷砖示例

顺便说一句,我相信有一种更简单的方法来声明我所有的20个按钮,也许有人能给我指出正确的方向

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.second_layout);

    Button btn1 = (Button)findViewById(R.id.btn1);
    Button btn2 = (Button)findViewById(R.id.btn2);
    Button btn3 = (Button)findViewById(R.id.btn3);
    Button btn4 = (Button)findViewById(R.id.btn4);
    Button btn5 = (Button)findViewById(R.id.btn5);
    Button btn6 = (Button)findViewById(R.id.btn6);
    Button btn7 = (Button)findViewById(R.id.btn7);
    Button btn8 = (Button)findViewById(R.id.btn8);
    Button btn9 = (Button)findViewById(R.id.btn9);
    Button btn10 = (Button)findViewById(R.id.btn10);
    Button btn11 = (Button)findViewById(R.id.btn11);
    Button btn12 = (Button)findViewById(R.id.btn12);
    Button btn13 = (Button)findViewById(R.id.btn13);
    Button btn14 = (Button)findViewById(R.id.btn14);
    Button btn15 = (Button)findViewById(R.id.btn15);
    Button btn16 = (Button)findViewById(R.id.btn16);
    Button btn17 = (Button)findViewById(R.id.btn17);
    Button btn18 = (Button)findViewById(R.id.btn18);
    Button btn19 = (Button)findViewById(R.id.btn19);
    Button btn20 = (Button)findViewById(R.id.btn20);



int tiles = 0;        
super.onCreate(savedInstanceState);
    TextView tv = (TextView)findViewById(R.id.calling_activity_info_text_view);

    String tileNum = getIntent().getExtras().getString("tilesNumber");

    String[] tileSet = {"A","B","C","D","E","F","G","H","I","J"};
    Button[] btnA = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13
            , btn14, btn15, btn16, btn17, btn18, btn19, btn20};

    if(tileNum.equals("4")) {
        tiles = Integer.parseInt(tileNum);
        tv.setText("" + tiles);
        for(int i = 0; i <= 3; i++){
            btnA[i].setVisibility(View.VISIBLE);
            btnA[i].setText(tileSet[i]);
        }

倒过来想想怎么样

从tileSet中取出项目; 用你刚拿的物品设置任意两个按钮; 从btnA中删除您设置的按钮,这样您就不会重复这些按钮; 冲洗并重复,直到填满tileNumber定义的所有按钮。 PS:tileNumber为什么应该是字符串?您可以同样轻松地将整数添加到意图中

PS2:您可以在for循环中以编程方式添加按钮,而不是实例化每个按钮,这将提高可伸缩性

编辑:

首先,要实现这一点,您可能需要将按钮数组替换为ArrayList,以便更容易删除元素。代码如下所示:

Random random = new Random();
for (int i = 0; i < tiles/2; i++) {

    for (int j = 0; j < 2; j++) {
       //Generates random number in the range [0, number of buttons)
        int buttonIndex = random.nextInt(btnA.length());

        // Get button from array
        btnA.get(buttonIndex).setText(tileSet[i]);

        //Remove button from button list
        btnA.remove(buttonIndex);
    }
}

当然,这段代码没有经过优化,也没有保存按钮单击的参考,但请告诉我它是否有助于解决问题。

就tileNumber而言,每当我尝试向意图中添加整数时,它都会给我一个错误。我还尝试在意图之前将字符串pars为int,这给了我一个错误。我已经为我的按钮做了一个循环,并移除了我的if Stations。就你的想法而言,这可能行得通,但我对如何做到这一点有点困惑。你能把代码粘贴到你设定意图的地方吗?每个原语都有一个方法,因此您应该能够设置一个整数而不会出现问题。感谢您的回复,很抱歉,我没有返回您之前的代码。我找到了一种方法,现在效果很好。我正在对应用程序进行最后的润色。谢谢你在我沮丧的时候帮助我,不用担心。但是如果你找到了一个好的解决方案,考虑把它的至少一部分粘贴在帮助这个问题的人身上,并把它标记为被接受的答案: