C#
콘솔프로그램으로 리스트출력
TOSUN
2018. 5. 28. 13:34
반응형
콘솔프로그램으로 값을 입력받아서 아래와 같은 결과를 출력합니다.
1. 입력받은 값을 표시
2. 입력받은 값을 큰숫자 순서로 재정렬해서 결과 표시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | string inputText = string.Empty; string strSpace = " "; string sortResult = string.Empty; StringBuilder sbResult = new StringBuilder(); List<string> sbResultList = new List<string>(); while (true) { Console.Write("정수를 입력하세요(end가 입력되면 반복 종료):"); inputText = Console.ReadLine(); if (inputText == "end") { break; } else { sbResult.Append(inputText + strSpace); sbResultList.Add(inputText); } } // [개행1] Console.WriteLine(string.Empty); // <결과1> Console.Write("입력한 리스트 [" + sbResult.ToString().Substring(0, sbResult.ToString().Length - 1) + "]"); // [개행2] Console.WriteLine(string.Empty); // <결과2> sbResultList.OrderBy(q => q).ToList(); sbResultList.Sort((a, b) => -1 * a.CompareTo(b)); foreach (string a in sbResultList) { sortResult += a + strSpace; } Console.Write("정렬한 리스트 [" + sortResult.Substring(0, sortResult.Length - 1) + "]"); | cs |
반응형