我的 for 循环有一些问题。我试过把它放在不同的地方,但还是没有运气。我正在做一个随机改变矩形不透明度的项目(我有红色、黄色、蓝色和绿色)。我让项目工作,以便它选择一种颜色,提高不透明度,等待,然后降低不透明度。但是,我想重复这一点,循环不会。
这是我的代码:
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
public void Start_Tapped_1(object sender, TappedRoutedEventArgs e)
{
loopthegame(5);
}
public void loopthegame(int amount)
{
for (int i = 0; i < amount; i++) {
startgame();
}
}
public async void startgame()
{
int randomcolor = RandomNumber(1, 8);
switch (randomcolor)
{
case 1:
Blue.Opacity = 1;
break;
case 2:
Red.Opacity = 1;
break;
case 3:
Yellow.Opacity = 1;
break;
case 4:
Green.Opacity = 1;
break;
case 5:
Blue.Opacity = 1;
break;
case 6:
Red.Opacity = 1;
break;
case 7:
Yellow.Opacity = 1;
break;
case 8:
Green.Opacity = 1;
break;
}
await Task.Delay(1000);
Blue.Opacity = 0.25;
Red.Opacity = 0.25;
Yellow.Opacity = 0.25;
Green.Opacity = 0.25;
}