2016-06-03 5 views
0

아래 코드를 첨부했습니다. Checker이라는 배열이 있는데 값을 전달하지만 루프가 반복 될 때 값을 얻지 못합니다.내 배열에 vales를 삽입했지만 배열이 비어 있습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int count = 0; 
      //String which have my coordinates 
      string TextFile = "[(27, 37), (27, 38), (27, 163), (27, 164), (27, 266), (27, 267), (27, 356), (27, 357), (27, 448), (27, 449), (27, 528), (27, 529), (28, 37), (28, 38), (28, 163), (28, 164), (28, 266), (28, 267), (28, 356), (28, 357), (28, 448), (28, 449), (28, 528), (28, 529), (51, 163), (51, 164), (51, 266), (51, 267), (51, 356), (51, 357), (51, 448), (51, 449), (52, 61), (52, 62), (52, 163), (52, 164), (52, 266), (52, 267), (52, 280), (52, 305), (52, 306), (52, 356), (52, 357), (52, 448), (52, 449), (52, 504), (52, 505), (53, 61), (53, 62), (53, 280), (53, 281), (53, 305), (53, 306), (53, 504), (53, 505), (355, 61), (355, 62), (355, 280), (355, 281), (355, 305), (355, 306), (355, 504), (355, 505), (356, 61), (356, 62), (356, 280), (356, 281), (356, 305), (356, 306), (356, 504), (356, 505), (380, 37), (380, 38), (380, 528), (380, 529), (381, 37), (381, 38), (381, 528), (381, 529)]"; 
      string splits = TextFile.TrimStart('['); 
      string[] split = TextFile.Split(')'); 

      string split_1 = null; 
      string split_2 = null; 
      string split_3 = null; 

      int pos = 0; 
      int lengthOfString = 0; 
      int valX, valZ, valX1, valZ1 = 0; 

      /* 
      * Getting the count of the coordinates in the array 
      */ 
      foreach (string x in split) 
      { 
       count++; 
      } 

      string[] stringArr = new string[count]; 

      /* 
      * Splitting the coordinates as x,y and store in an array 
      */ 
      foreach (string coord in split) 
      { 
       split_1 = coord; 
       split_1 = split_1.Trim('['); 
       split_1 = split_1.Trim('('); 
       split_1 = split_1.Trim(','); 
       split_1 = split_1.Trim(' '); 
       split_1 = split_1.Trim('('); 
       split_1 = split_1.TrimEnd(']'); 
       stringArr[pos] = split_1; 
       pos++; 
      } 

      Console.WriteLine("Array Length " + stringArr.Length); 

      /* 
      * extracting simalar x coordinates 
      */ 
      Console.WriteLine(""); 
      Console.WriteLine("-----------------extracting simalar x coordinates----------------"); 
      Console.WriteLine(""); 
      int[] checker = new int[count]; 
      //checker[0] = 37; 
      int indexX = 0; 
      int loopRunX = 0; 
      for (int a = 0; a < stringArr.Length - 1; a++) 
      { 
       Console.WriteLine(""); 
       Console.WriteLine("loop begining"); 
       Console.WriteLine("Checker value loop 1 : " + checker[a]); 
       split_2 = stringArr[a]; 
       lengthOfString = split_2.Length; 

       valX = int.Parse(split_2.Substring(0, split_2.IndexOf(','))); 
       valZ = int.Parse(split_2.Substring(split_2.IndexOf(' '), (lengthOfString - split_2.IndexOf(' ')))); 
       Console.WriteLine("Checker value loop 1: " + checker[a]); 
       if (checker[a] != valZ) 
       { 
        Console.WriteLine("valZ " + valZ); 
        checker[a] = valZ; 
        Console.WriteLine("Checker value if : " + checker[a]); 
        int countx = 0; 
        for (int x1 = a; x1 < stringArr.Length - 1; x1++) 
        { 
         split_3 = stringArr[x1]; 
         lengthOfString = split_3.Length; 

         valX1 = int.Parse(split_3.Substring(0, split_3.IndexOf(','))); 
         valZ1 = int.Parse(split_3.Substring(split_3.IndexOf(' '), (lengthOfString - split_3.IndexOf(' ')))); 

         //Check for the simillar x in the text file we provide 
         if (valZ == valZ1) 
         { 
          countx++; 
          Console.WriteLine("Y is " + valZ + " and the coordinates which have simillar z ==> (" + valX1 + ", " + valZ1 + "). Index is " + x1 + " Count is " + countx); 
         } 
        } 
        loopRunX++; 
        indexX = indexX + countx; 
        Console.WriteLine("Next Index to check onwards : " + indexX); 
        Console.WriteLine("Looping Count : " + loopRunX); 

        Console.WriteLine("Checker value outter loop 2: " + checker[a]); 

       } 
       Console.WriteLine("Checker value after loop 2: " + checker[a]); 
       Console.WriteLine(""); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

이것은 내가 얻은 결과입니다.

Result

코드가 한번 실행

37은 검사기 배열에 삽입한다. 다음 반복에서 그 배열은 비게됩니다. 나는 어디로 잘못 갔는가? 내가 제대로 이해하면

+0

loopRunX> 0 일 때 "Checker value loop 1 : 0"이 "Checker value loop 1 : 37"이 될 것으로 예상됩니다. – DAXaholic

+2

여기에서 화재. 나는 훨씬 더 쉬운 해결책이 있다고 생각한다. 문제가 아닌, 당신이 달성하고자하는 것을 설명 할 수 있겠는가? –

+0

특정 C# 식의 동작을 이해할 수 없거나 무언가를 표현하는 방법을 모르는 경우와 같이보다 중점적으로 질문하십시오. 그렇지 않으면 주제와 다르게 닫힙니다. 이것은 "코딩 도움"커뮤니티가 아닙니다. –

답변

1


루프가의 값을 시작
이 라인 69 & 0
이며,이

  string TextFile = "[(27, 37), (27, 38), (27, 163), (27, 164), (27, 266), (27, 267), (27, 356), (27, 357), (27, 448), (27, 449), (27, 528), (27, 529), (28, 37), (28, 38), (28, 163), (28, 164), (28, 266), (28, 267), (28, 356), (28, 357), (28, 448), (28, 449), (28, 528), (28, 529), (51, 163), (51, 164), (51, 266), (51, 267), (51, 356), (51, 357), (51, 448), (51, 449), (52, 61), (52, 62), (52, 163), (52, 164), (52, 266), (52, 267), (52, 280), (52, 305), (52, 306), (52, 356), (52, 357), (52, 448), (52, 449), (52, 504), (52, 505), (53, 61), (53, 62), (53, 280), (53, 281), (53, 305), (53, 306), (53, 504), (53, 505), (355, 61), (355, 62), (355, 280), (355, 281), (355, 305), (355, 306), (355, 504), (355, 505), (356, 61), (356, 62), (356, 280), (356, 281), (356, 305), (356, 306), (356, 504), (356, 505), (380, 37), (380, 38), (380, 528), (380, 529), (381, 37), (381, 38), (381, 528), (381, 529)]"; 
      TextFile = TextFile.Trim(new char[] { '[', ']' }); 
      int[][] data = TextFile.Split(new char[] { '('}, StringSplitOptions.RemoveEmptyEntries) 
       .Select(x => x.Trim()).Select(x => x.Trim(new char[] { ',' })).Select(x => x.Trim(new char[] { ')' })) 
       .Select(y => y.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) 
       .Select(z => z.Select(a => int.Parse(a)).ToArray()).ToArray(); 
1

을보십시오 (75) 인쇄 검사기 [A] 즉, 체커의 값 [0 ] 즉 0입니다.
그리고 줄 79는 체커 [a], 즉 체커 [0]에 값을 할당합니다.

체커 값이 지정되기 전에 인쇄하고 있습니다.

관련 문제