如果数组中没有剩余的零,如何打破循环?(C#)

时间:2016-10-14 08:22:11

标签: 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[,] 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();
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

ScriptManager.RegisterStartupScript(this,GetType(), "Message", "showModal();", true); 语句终止它出现的最近的封闭循环或switch语句。 Control被传递给终止语句之后的语句。 More