C#程序检查有效值

我需要一些帮助来从以下程序中删除重复的值。 我不能使用Hashset,列表或除arrays之外的任何东西。 我已经查看了可以使用Hashset和list的不同解决方案,但我不允许使用它。 有人可以帮我弄这个吗。 该程序假设在a到j之间只取5个值并删除任何重复值:

class Program { static void main(string[] args) { char[] Array = new char[5]; Console.WriteLine("Please Enter 5 Letters B/W a through j only: "); string letters = "abcdefghij"; for (int i = 0; i < Array.Length;) { string lower = Console.ReadLine().ToLower(); if (letters.Contains(lower) && lower.Length == 1) { Array[i] = Convert.ToChar(lower); i++; } else { Console.WriteLine("You have entered an incorrect value); continue; } } Console.WriteLine("You have Entered the following Inputs: "); for (int i = 0; i<Array.Length; i++) { Console.WriteLine(Array[i]); } } } 

我认为这对你有用。

 static void Main() { char[] array = new char[5]; Console.WriteLine("Please Enter 5 Letters B/W a through j only: "); string letters = "abcdefghij"; int counter = 0; while (counter < 5) { string input = Console.ReadLine().ToLower(); char myChar = input[0]; if (input.Length != 1 || array.Contains(myChar) || !letters.Contains(myChar)) { Console.WriteLine("You have entered an incorrect value"); continue; } array[counter++] = myChar; } Console.WriteLine("You have Entered the following Inputs: "); Console.WriteLine(string.Join(", ", array)); } 
  static void Main(string[] args) { char[] Array = new char[5]; Console.WriteLine("Please Enter 5 Letters B/W a through j only: "); string letters = "abcdefghij"; char[] read = Console.ReadLine().ToLower().ToCharArray(); //loop through array for (int i = 0; i < 5; i++) { if (letters.Contains(read[i]) && !Array.Contains(read[i])) { Array[i] = read[i]; } else { Console.WriteLine("You have entered an incorrect value"); } } Console.WriteLine("You have Entered the following Inputs: "); for (int i = 0; i < Array.Length; i++) { Console.WriteLine(Array[i]); } Console.ReadKey(); } 

该代码应该做你想要的

如果只允许使用数组,有很多方法可以解决这个问题。 一种选择是利用字符串的内部字符数组并执行不区分大小写的查找以确保输入的字符有效。 并再次使用字符串的内置字符数组,只为最终结果添加唯一字符。

 static void Main() { const string msg = "Only 5 letters between a and j are allowed."; const string letters = "abcdefghij"; Console.WriteLine( "Please Enter 5 Letters B/W a through j only:" ); // read the entire input into a string string input = Console.ReadLine(); // verify the input is the correct length. if( input == null || input.Length != 5 ) { Console.WriteLine( msg ); return; } // store the final result w/o duplicates in a string string result = string.Empty; // we know there are 5 and only 5 characters in the input string, // so just iterate over the entire string. foreach( char c in input ) { string value = c.ToString(); // check if the character exists in the allowable characters using // a case insensitive lookup. if( letters.IndexOf( value, StringComparison.OrdinalIgnoreCase ) < 0 ) { // an invalid character was found, no need to continue. Console.WriteLine( value + " is inavlid." + msg ); return; } // add the original character to the result string if it has yet to be added. if( !result.Contains( value ) ) { result += value; } } Console.WriteLine( "You have entered the following inputs:" ); Console.WriteLine( result ); }