如何检查列表中的两个按钮是否具有相同的不透明度



我的应用程序有问题。我正在创建一个";简单的";我有一个不透明度为0.4的按钮列表,我在列表中随机选择一个按钮,并将其不透明度设置为1。我的观点是生成两个按钮,所以现在我只在开始时调用了两次函数,但问题是,当两个按钮相同时,它只生成一个。我希望你能理解(我是法国人(。

'''        void ChooseFirstRandomButton()
{
var rand = new Random();
var buttonList = new List<Button> { 
one, two, three, four, five, six, seven, eight, nine,
ten, eleven, twelve, thirteen, fourteen, fifteen,
sixteen, seventeen, eighteen, nineteen, twenty,
twentyone, twentytwo, twentythree, twentyfour
};
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}
'''

";一"两个"。。。是我纽扣的名字。

谢谢你的回答。

使用循环继续拾取数字,直到获得尚未选择的

// initialize your list of buttons and random seed 
// before the loop
bool loop = true;
while (loop)
{
int index = rand.Next(buttonList.Count);
var randomButton = buttonList[index];
// if it's not set, set it and exit loop
if (randomButton.Opacity != 1) 
{
randomButton.Opacity = 1;
loop = false;
}
}

根据您提供的视频和您的描述"按钮彼此相遇";你只需要改进你的算法:

  1. 在函数之外的某个地方创建一个全局变量:
int opacity_1_index = -1;
  1. 选择随机索引后,检查它是否与记忆中的索引不相等:
int index = rand.Next(buttonList.Count);
if (index == opacity_1_index)
{
// exit function and call it again
}
  1. 否则请记住此索引并执行其余操作:
else
{
opacity_1_index = index; // add this line
var randomButton = buttonList[index];
var randomButtonOpacity = randomButton.Opacity;
randomButton.Opacity = 1;
}

相关内容

最新更新