Пишу программу, которая сравнивает строку из текстового файла с строкой, введенной с клавиатуры. Но при первом проходе в цикле игнорируется строка ввода (пометил комментарием), после первой итерации ввод работает корректно. С чем это может быть связано?
P.S. Тестирую на английском, т.к. не знаю немецкого.
class Program
{
public static string PathSetRus()
{
string a;
Console.WriteLine("Введите путь для файла русских слов:");
a = Console.ReadLine();
return a;
}
public static string PathSetGer()
{
string a;
Console.WriteLine("Введите путь для файла немецких слов:");
a = Console.ReadLine();
Console.WriteLine();
return a;
}
public bool IsValid(char ch)
{
if (ch < '1' | ch > '2') return true;
else return false;
}
public static bool TrCh()
{
bool flag=true;
char ins;
Program y = new Program();
try
{
for(; ; )
{
do
{
Console.WriteLine("Выберите режим перевода (1 или 2):\n1.Русский-Немецкий\n2.Немецкий-Русский");
do
{
ins = (char)Console.Read();
}
while (ins == '\n' | ins == '\r');
}
while (y.IsValid(ins));
Console.WriteLine();
switch (ins)
{
case ('1'):
flag = true;
break;
case ('2'):
flag = false;
break;
}
break;
}
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
return flag;
}
static void Main()
{
string r = PathSetRus(); //Путь для рус.слов
string g = PathSetGer(); //Путь для нем.слов
bool f = TrCh(); //Выбранный режим перевода
string line1 = "", line2, ans = "";
if (f)
{
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader rus = new StreamReader(r);
StreamReader ger = new StreamReader(g);
while (line1 != null)
{
//Read the line of text
line1 = rus.ReadLine();
line2 = ger.ReadLine();
while ((line1 != null) && (ans != line2))
{
Console.WriteLine("Переведите на немецкий слово " + line1);
ans = Console.ReadLine(); //Строка игнорируется при первой итерации
Console.WriteLine();
if (ans != line2)
{
Console.WriteLine("Ошибка,попробуйте еще");
Console.WriteLine();
}
}
}
rus.Close();
ger.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Вы отлично поработали!");
}
}

TrCh()покажите этот метод, проблема в нем – aepot Oct 29 '22 at 15:04