I want to exit from loop when both i and j have the value 6. but it exits when and one of them have value 6.
int i,j,k;
i=k=0;
j=1;
Random num = new Random();
Console.WriteLine("Please Press any Key to Roll");
while((i!=6)&&(j!=6))
{
Console.ReadKey();
i= num.Next(0,7);
j= num.Next(0,7);
Console.WriteLine("1st Rolled Number is: " i);
Console.WriteLine("2st Rolled Number is: " j);
k ;
}
Console.WriteLine("Your have achieved it in " k " Atempts");
CodePudding user response:
To exit the loop when both i and j have the value 6, you can change the condition in the while loop to
Change too
while ((i != 6) || (j != 6))
This will exit the loop when either i or j. To exit the loop when both i and j have the value 6,
while (!(i == 6 && j == 6))
This will exit the loop when both i and j have the value 6.
CodePudding user response:
In other words, the loop should continue if either i
isn't 6 or j
isn't 6 - this is a logical or (||
) condition, not a logical and (&&
) condition:
while ((i != 6) || (j != 6))