如果存在';数组中没有剩余的零?(C#)

如果存在';数组中没有剩余的零?(C#),c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,用户将玩一个匹配的游戏。如果项目与数字匹配,它将说它匹配,否则,它将不匹配。所有匹配的数字都在matchedNum数组上传递。matchedNum数组的初始值为零。当数组中没有更多的零时,如何停止循环 class Program { public static int[,] memoryNum = { { 2, 1, 7, 3 }, { 5, 4, 9, 6 }, { 3, 7, 2, 4 }, { 6, 5, 1, 9 } }; public static int[,] mat

用户将玩一个匹配的游戏。如果项目与数字匹配,它将说它匹配,否则,它将不匹配。所有匹配的数字都在matchedNum数组上传递。matchedNum数组的初始值为零。当数组中没有更多的零时,如何停止循环

class Program
{
    public static int[,] memoryNum = { { 2, 1, 7, 3 }, { 5, 4, 9, 6 }, { 3, 7, 2, 4 }, { 6, 5, 1, 9 } };
    public static int[,] matchedNum = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
    public static int row1 = 0;
    public static int col1 = 0;
    public static int row2 = 0;
    public static int col2 = 0;
    public Boolean matches = true;
    public static int tries = 3;
    static void Main(string[] args)
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                Console.Write("* ");
                if (j == 3)
                {
                    Console.WriteLine("");
                }
            }
        }

        while (tries != 0)
        {
            Console.Write("\nEnter row(First Num): ");
            row1 = Convert.ToInt32(Console.ReadLine());
            checker(row1);
            Console.Write("Enter col(First Num): ");
            col1 = Convert.ToInt32(Console.ReadLine());
            checker(col1);
            Console.Write("Enter row(Second Num): ");
            row2 = Convert.ToInt32(Console.ReadLine());
            checker(row2);
            Console.Write("Enter col(Second Num): ");
            col2 = Convert.ToInt32(Console.ReadLine());
            checker(col2);

            if (memoryNum[row1 - 1, col1 - 1] == memoryNum[row2 - 1, col2 - 1])
            {
                matchedNum[row1 - 1, col1 - 1] = memoryNum[row1 - 1, col1 - 1];
                matchedNum[row2 - 1, col2 - 1] = memoryNum[row2 - 1, col2 - 1];
                displayMatched(matchedNum);
                Console.WriteLine("Matched!");
            }
            else
            {
                displayNotMatched(memoryNum);
                tries--;
                Console.WriteLine("Did not match, Please try again!");
            }

            //if there's no zero in the array, break;
        }


        if(tries == 0) 
            Console.WriteLine("Number of tries exceeded! Play Again?");
        Console.ReadKey();
    }
}
类程序
{
公共静态int[,]memoryNum={{2,1,7,3},{5,4,9,6},{3,7,2,4},{6,5,1,9};
公共静态int[,]matchedNum={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0};
公共静态int row1=0;
公共静态int col1=0;
公共静态int row2=0;
公共静态int col2=0;
公共布尔匹配=true;
公共静态int=3;
静态void Main(字符串[]参数)
{
对于(int i=0;i<4;i++)
{
对于(int j=0;j<4;j++)
{
控制台。写(“*”);
如果(j==3)
{
控制台。写线(“”);
}
}
}
while(尝试次数!=0)
{
控制台。写入(“\n输入行(第一个数字):”;
row1=Convert.ToInt32(Console.ReadLine());
检查器(第1行);
控制台。写入(“输入列(第一个数字):”;
col1=Convert.ToInt32(Console.ReadLine());
检查器(col1);
Console.Write(“输入行(第二个数字):”;
row2=Convert.ToInt32(Console.ReadLine());
检查器(第2行);
控制台。写入(“输入列(第二个数字):”;
col2=Convert.ToInt32(Console.ReadLine());
检查器(col2);
if(memoryNum[row1-1,col1-1]==memoryNum[row2-1,col2-1])
{
matchedNum[row1-1,col1-1]=memoryNum[row1-1,col1-1];
matchedNum[row2-1,col2-1]=memoryNum[row2-1,col2-1];
显示匹配(matchedNum);
控制台。WriteLine(“匹配!”);
}
其他的
{
显示不匹配(memoryNum);
尝试--;
Console.WriteLine(“不匹配,请重试!”);
}
//如果数组中没有零,则中断;
}
如果(尝试==0)
Console.WriteLine(“已超过尝试次数!是否再次播放?”);
Console.ReadKey();
}
}

使用计数器,当它是16时,没有更多的零可查找。

使用计数器,当它是16时,没有更多的零可查找。

break语句终止它出现的最近的封闭循环或switch语句。控件传递给终止语句后面的语句

break语句终止它出现的最近的封闭循环或switch语句。控件传递给终止语句后面的语句

Use它终止它出现的最近的封闭循环或switch语句。控件被传递到终止语句后面的语句(如果有)。@Mohithrivastava我知道我需要使用break,但我不知道如何使用for循环。如果我尝试“If(matchedNum[i][j]!=0)break;”,之后它将忽略其余的数字。我在想如果这个数字后面有零怎么办?你是在问你的
中断
if
条件吗?因为从评论的部分来看,我想你已经知道你需要以某种方式打破循环。@uteist是的!我的问题已经回答了。我只需要使用一个计数器hahathhow,你可以添加
intzeroccounter=0
然后在每次匹配时,删除一个并将计数器增加1。然后,您可以在需要时使用if和break来检查它。使用它可以终止它出现的最近的封闭循环或switch语句。控件被传递到终止语句后面的语句(如果有)。@Mohithrivastava我知道我需要使用break,但我不知道如何使用for循环。如果我尝试“If(matchedNum[i][j]!=0)break;”,之后它将忽略其余的数字。我在想如果这个数字后面有零怎么办?你是在问你的
中断
if
条件吗?因为从评论的部分来看,我想你已经知道你需要以某种方式打破循环。@uteist是的!我的问题已经回答了。我只需要使用一个计数器hahathhow,你可以添加
intzeroccounter=0
然后在每次匹配时,删除一个并将计数器增加1。然后你可以用if检查,需要时可以中断。谢谢!正是我需要的!我忘了计数器是存在的,虽然计数器应该等于8,因为它会增加每个匹配的数字,因为只有8个匹配的数字,但谢谢!非常感谢。正是我需要的!我忘了计数器是存在的,虽然计数器应该等于8,因为它会增加每个匹配的数字,因为只有8个匹配的数字,但谢谢!