2016-03-26 6 views
0

文字を入力のみとし、重複しないプログラムを作成しようとしています。入力に1文字入力するとエラーになります。 これは私が何をする必要があるのですか、私は、入力行や入力などの各行にユーザ入力を必要とします。もし重複値があれば、エラーメッセージが必要です。間違った値です。そのようなエラーが表示されます。私はLINQ、Hashset、またはリストを使用することはできません。配列でなければなりません。C#別の行に入力が必要です

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

あなたの考えは5文字を連続して取得することですか?考えられる解決策の1つは、do.whileループでConsole.ReadLineをラップすることです。ユーザーからの入力ごとに入力をループして、有効なエントリがないかどうかを確認できます。それぞれの文字のループ内で5文字をループし、それらをハッシュセットに追加すると、一意の値のみを許可するハッシュセット、ハッシュセットは5つの要素を持つ必要があります。ユーザに有効な文字の組を入力させる。検証後、ループから抜け出してユーザー入力を処理することができます。 – rmjoia

+0

実際にはContains()はLinq拡張です。 – derloopkat

+0

[C#で特殊文字や数字を使わずに文字(az)のみでユーザー入力を要求する方法](http://stackoverflow.com/questions/36191717/how-to-ask-for-user-input- with-letters-az-only-without-special-characters-or) –

答えて

-1

これは、(i = 0をint型;私< 5;私は++)のためにあなたの問題

ある

これはあなたの修正です:

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 < read.Length; 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(); 
      } 
0

私はこれ以上だと思いますあなたが望むものはそれ以下である:

var max = 5; 
var array = new char[max]; 
var letters = "abcdefghij"; 

var count = 0; 
while (count < 5) 
{ 
    Console.WriteLine("Please Enter {0} Letters B/W a through j only: ", max); 

    var key = Console.ReadKey(); 
    var read = key.KeyChar 

    if (!letters.Contains(read)) 
    { 
     Console.WriteLine("You have entered an incorrect value"); 
     continue; 
    } 

    var found = false; 
    for (int i = 0; i < count; i++) 
    { 
     if (array[i] == read) 
     { 
      found = true; 
     } 
    } 

    if (found) 
    { 
     Console.WriteLine("You have entered an duplicate value"); 
     continue; 
    } 

    array[count++] = read; 
} 

Console.WriteLine("You have Entered the following Inputs: "); 
for (int i = 0; i < array.Length; i++) 
{ 
    Console.WriteLine(array[i]); 
} 

Console.ReadKey(); 
0

ユーザーが値を個別に入力するようにするには、ループ内の各文字を要求する必要があります。 次のようなもの:

static void Main(string[] args) 
{ 
    const string validValues = "abcdefghij"; 
    var enteredCharacters = new char[5]; 
    for (int i = 0; i < enteredCharacters.Length; i++) 
    { 
     Console.WriteLine("Please enter a unique character between a and j"); 
     var input = Console.ReadLine(); 
     if (input.Length == 0) 
     { 
      Console.WriteLine("You did not enter a value."); 
      return; 
     } 
     if (input.Length > 1) 
     { 
      Console.WriteLine("You have entered more than 1 character"); 
      return; 
     } 
     var character = input[0]; 
     if (!validValues.Contains(character)) 
     { 
      Console.WriteLine("You have entered an invalid character"); 
      return; 
     } 
     if (enteredValues.Contains(character)) 
     { 
      Console.WriteLine("You have already entered this character"); 
      return; 
     } 
     enteredCharacters[i] = character; 
    } 
    // process numbers. 
} 
関連する問題