First off, you have a parentheses mismatch. Are you trying to do:
public void machine()
{
Random RandomClass = new Random();
int a = RandomClass.Next(1,9);
if(a==1)
{
if (pictureBox1.Image == img0) {
pictureBox1.Image = img2;
}
}
else
{
machine();
}
}
or this:
public void machine()
{
Random RandomClass = new Random();
int a = RandomClass.Next(1,9);
if(a==1)
{
if (pictureBox1.Image == img0) {
pictureBox1.Image = img2;
}
else
{
machine();
}
}
}
As stated by many users, you don't need to keep creating a new Random
object. In fact, you will be much better off with a loop than with recursion in this instance.
Here is an example:
public void machine()
{
Random RandomClass = new Random();
// Not sure what the point of the random is but whatevs
int a = RandomClass.Next(1,9);
for (; a != 1; a = RandomClass.Next(1,9));
if (pictureBox1.Image == img0) {
pictureBox1.Image = img2;
}
}
Note: While highly unlikely, there is no guarantee that this function will ever end. However, using a loop rather than recursion will simply cause your computer to respond a little worse and the fan to go nuts rather than throw a Stack Overflow
exception.
Hope this helps and good luck!