用regex來處理吧﹐它是專門做這種事情的,如以下就是只能輸入1~5數字的檢查 Regex regexCol = new Regex("^([1-5])$"); bool flagcol = false; while (!flagcol) { Console.Write("Number of Columns:"); entercol = Console.ReadLine(); if (regexCol.IsMatch(entercol)) { col = Convert.ToInt32(entercol); flagcol = true; } else { Console.WriteLine("Cannot create spreadsheet:the maximum number of columns are 5 respectively"); } }
public string Replace (string oldValue,string newValue)
Regex regexCol = new Regex("^([1-5])$");
bool flagcol = false;
while (!flagcol)
{
Console.Write("Number of Columns:");
entercol = Console.ReadLine();
if (regexCol.IsMatch(entercol))
{
col = Convert.ToInt32(entercol);
flagcol = true;
}
else
{
Console.WriteLine("Cannot create spreadsheet:the maximum number of columns are 5 respectively");
}
}