2016-09-10 10 views
-1

좋은 방법으로 짧은 문자열을 부울 값으로 생각하면 좋았습니다. A = 0, B = 1 ...각 문자의 알파벳순 부채꼴 표현 방법

나는 코드를 잘못 만들었지 만 잘못된 대답은 내가 어디 있는지 알고 있다고 생각하지만 문제는 무엇인지 알지 못합니다. 그래서 여기

코드입니다 :

void Start() 
{ 
    i = Inventory.instance; 
    List<Items> items = i.returnList("Food"); 
    Debug.Log(items.Count); List<Items> sItems = GetListSetList(items); 

    foreach (Items item in sItems) 
    { 
     Debug.Log(item.name); 
    } 
} 

목록 항목 = i.returnList ("식품"); < -이 부분은 재고 목록에서 모든 "식품"테거 드 상품을 반환하는 곳입니다. 코드는 여기에 있습니다 :

목록 sItems = GetListSetList (items); < - 이것은 내가 재고 목록에서 얻은 짧은 목록을 실제로 시도하는 부분입니다. 이 모양은 다음과 같습니다.

private List<Items> GetListSetList(List<Items> itemS) 
{ 
    foreach (Items item in itemS) 
    { 
     GetAlphabetaValue(item); 
    } 
    List<Items> shorted = new List<Items>(); 
    Items[] hold = new Items[itemS.Count]; 
    foreach (Items item in itemS) 
    { 
     for (int x = 0; x < hold.Length; x++) 
     { 
      if (hold[x] == null) 
      { 
       hold[x] = item; 
       break; 
      } 
      else 
      { 
       bool PassIt = true; 
       for (int c_value = 0; c_value < item.Alphabet_value.Length; c_value++)      
        if (item.Alphabet_value[c_value] > hold[x].Alphabet_value[c_value])       
         PassIt = false;      

       if (PassIt) 
       { 
        for (int h_pos = hold.Length - 1; h_pos > x; h_pos--)      
         if (hold[h_pos] != null) 
          hold[h_pos] = hold[h_pos - 1];      

        hold[x] = item; 
        break; // If i use this break i get error ("NullReferenceException") 
       } 
       else continue;        
      } 
     } 
    } 
    for (int x = 0; x < hold.Length; x++)   
     shorted.Add(hold[x]);   
    return shorted; 
} 

시작이 void는 모든 항목 이름 문자열의 모든 문자에 값을 부여합니다. 이 부분은 다음과 같습니다. GetAlphabetaValue (item); 난 당신이 내가 말하려고 이해 바랍니다

private void GetAlphabetaValue(Items x) 
{ 
    x.Alphabet_value = new int[x.name.Length]; 
    for (int c = 0; c < x.Alphabet_value.Length; c++) 
    { 
     string character = x.name.Substring(c, 1); 
     character.ToLower(); 
     switch (character) 
     { 
      case "a": 
       x.Alphabet_value[c] = 0; 
       break; 
      case "b": 
       x.Alphabet_value[c] = 1; 
       break; 
      case "c": 
       x.Alphabet_value[c] = 2; 
       break; 
      case "d": 
       x.Alphabet_value[c] = 3; 
       break; 
      case "e": 
       x.Alphabet_value[c] = 4; 
       break; 
      case "f": 
       x.Alphabet_value[c] = 5; 
       break; 
      //To the end 
     } 
    } 
} 

: 아, 그리고 미안 AlphaBeta :) 좋아요를 명명하는 것은 내가 어떻게이 값을 얻는 것은 이것이다 D 감사합니다 :) 그리고 내가 시작하기 전에 인터넷에서 어떤 정보를 찾을 시도 이 일을,하지만 난 정말 배열에서 여러 문자열을 짧게 수있는 방법을 찾으십시오.

이 부분은 제가 잘못 찾은 것 같아하지만 지금 난 그냥 잘못 오순절이 있다는 것을 볼 수 없습니다 :

for (int h_pos = hold.Length - 1; h_pos > x; h_pos--) 
     if (hold [h_pos] != null) 
     { 
     hold [h_pos] = hold [h_pos - 1];       
     hold [x] = item; 
     }   

답변

0

은 당신이 정말로 그것을가 20 switch-case이 좋은 아이디어라고 생각하지으로 시작하려면?

문자는 이미 번호가 지정되어 있습니다. a-z97-122 문자에 해당하는 문자로 Unicode에 해당합니다.

이 기능 :

void GetAlphabetaValue(Items x) 
{ 
    x.Alphabet_value = new int[x.name.Length]; 
    for (int c = 0; c < x.Alphabet_value.Length; c++) 
    { 
     string character = x.name.Substring (c, 1); 
     character.ToLower(); 
     switch (character) 
     { 
     case "a": 
     x.Alphabet_value [c] = 0; 
     break; 
     ///Etc... //Etc..... And end. 
     } 
    } 
}  

이되다 :

void GetAlphabetaValue2(Items x) 
{ 
    var CharIntList = List<int>; 
    foreach (char ch in x.Name) 
     CharIntList.Alphabet_value.Add(ch - 97); 
    x.Alphabet_value = CharIntList.ToArray(); 
} 

훨씬 간단합니다. 또한 코드는 더러스럽고 이해하기 어렵고 형식이 잘못되었습니다. 아마 당신은 C#에 익숙하지 않으므로 코드를 작성하는 방법에 대해 읽어야합니다. 잘하는 것처럼 보이지 않지만 다른 pepole은 코드를 이해할 수 있어야합니다.

질문에 대해서는 sort이 아니라고 생각합니다. short (다른 것들이 다릅니다). 또한 Items은 복수형으로 표시되는 이유는 무엇입니까? 그것은 단 한 가지입니다. 그렇다면 Item이어야합니다.

글쎄, 나는 당신의 문제에 대해 잘 모르지만, 당신이 당신의 GetListSetList() 기능을 대체 할 수

private List<Items> GetListSetList(List<Items> items) 
{ 
    foreach (Items item in items) 
    { 
     GetAlphabetaValue(item); 
     Array.Sort(item.Alphabet_value); 
    } 
    return items; 
} 
+0

와우, 감사합니다 .. 나는 더 나은 방법이 멀티 스위치 케이스 문보다 그렇게 생각했습니다 :) 그 점에 대해 감사드립니다. –

+0

'int [] Alphabet_value'에서 값을 '정렬'하는 코드가 있습니까? – null

+0

그리고 네, 그 마지막 부분은 제가 여러 번 보았던 것입니다,하지만 저는 class (item.Alphabet_value)의 지정된 부분을 짧게 사용할 수 있다는 것을 알지 못합니다. 나는 Short가 짧은 것 (item)처럼 작동한다고 생각했다; :) –

0

것은이

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Items> items = Items.returnList("Food"); 
      var groups = items.GroupBy(x => x.name.Substring(0,1)).ToList(); 

     } 
    } 
    public class Items 
    { 
     public static List<Items> items = new List<Items>(); 
     public string name { get; set; } 
     public string type { get; set; } 


     public static List<Items> returnList(string type) 
     { 
      return items.Where(x => x.type == type).ToList(); 
     } 

    } 

} 
+0

고마워. 그것의 좋은 foreach 루프를 없애 버려 :) –

1

사용이 코드 같은 것을보십시오.

 string str = "Tamil"; 
     List<char> list = str.ToList(); 
     list = list.OrderBy(x => x.ToString()).ToList(); 
     foreach (var item in list) 
     { 
      Console.WriteLine(item); 
     } 
     Console.ReadLine();