int.Parse(Console.ReadLine()) 若用戶輸入非數字文本(如 "abc"、空字符串、含空格),拋出 FormatException。
string input = Console.ReadLine();
if (int.TryParse(input, out int value)) {
// 轉換成功,使用 value
} else {
Console.WriteLine("請輸入有效整數");
}
Trim() 去除首尾空白。long.TryParse,浮點數用 double.TryParse。防御性編程習慣,讓程序更健壯友好。