我在這裏有這個C#控制檯應用程序代碼,用於從文件中讀取文本。 當用戶輸入一個值時,它會在文件中搜索包含該值的行。 在我的情況下,控制檯會詢問房間號,控制檯將然後搜索room.txt的房間號那與分裂「」C#控制檯應用程序在while循環中顯示無效輸入
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
string roomNumber;
Console.WriteLine("Enter room number");
roomNumber = Console.ReadLine();
// Read the file and display it line by line.
System.IO.StreamReader file = new
System.IO.StreamReader("room.txt");
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(',');
if (roomNumber == words[1])
{
Console.WriteLine(line);
}
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
}
}
}
我如何讓這個會寫「無效的房間號碼「,當它無法在文本文件中找到它,然後循環回問房間號碼。
這看起來非常像一個家庭作業... –