2016-10-14 2 views
0

사용자가 일치하는 게임을 재생합니다. 항목이 숫자와 일치하면 다른 항목과 일치한다고 말하고 일치하지 않습니다. 일치하는 숫자는 모두 matchedNum 배열에 전달됩니다. matchedNum 배열의 초기 값은 0입니다. 배열에 더 이상 0이 없을 때 어떻게 루프를 멈 춥니 까?배열에 0이 남아 있지 않은 경우 루프를 끊는 방법? (C#)

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(); 
    } 
} 
+0

[Break;] (https://msdn.microsoft.com/en-us/library/adbctzc4.aspx)를 사용하면 나타나는 가장 가까운 루프 또는 switch 문을 종료합니다. 제어는 종료 된 명령문 (있는 경우) 다음에 오는 명령문으로 전달됩니다. –

+0

@MohitShrivastava 내가 휴식을 사용해야하지만 나는 for 루프를 사용하여 어떻게 해야할지 모르겠다. 만약 내가 '(matchedNum [i] [j]! = 0) break;'를 시도하면 그 이후의 나머지 숫자는 무시할 것입니다. 그 번호 뒤에 0이 있다면 어떻게 될까? – Temmie

+0

'break'에 대해'if' 조건을 요구하고 있습니까? 코멘트 부분에서 사촌, 나는 당신이 이미 당신이 어떻게 든 루프를 '휴식'해야 알아. – uTeisT

답변

2

카운터를 사용하면 16 일 때 더 이상 0을 찾을 수 없습니다.

+0

고마워요! 내가 필요한 것! 카운터가 존재한다는 것을 잊었습니다. – Temmie

+0

카운터는 8과 일치해야하지만, 8 개의 일치하는 숫자 만 있지만 감사합니다. – Temmie

0

break 문은 나타나는 가장 가까운 루프 또는 switch 문을 종료합니다. 제어는 종료 된 명령문 다음의 명령문으로 전달됩니다. More

관련 문제