我的輸入被「TryParse」檢查。如果它是假的,我想讓用戶正確輸入。但是,當我點擊任何按鍵後,它會刪除上面的所有「Console.Clear();」的原因。 - 我只想刪除輸入部分,而不是上面的部分。讓用戶在不干擾其他字符串的情況下重新輸入輸入(C#)
namespace Practice
{
class Program
{
static void Main(string[] args) // Main Method
{
byte age1, age2;
string input;
do
{
Console.Write("Enter Age 1: "); input = Console.ReadLine();
if (!byte.TryParse(input, out age1)) Error();
} while (!byte.TryParse(input, out age1));
do
{
Console.Write("Enter Age 2: "); input = Console.ReadLine();
if (!byte.TryParse(input, out age1)) Error();
} while (!byte.TryParse(input, out age2));
}
static void Error()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Invalid Input...");
Console.ResetColor();
Console.ReadKey();
Console.Clear();
}
}
}
你在問你的代碼風格嗎? – Toumash 2014-12-04 21:15:57
'byte age1,age2'應該是'int ||' Int32'也請修正格式不正確的代碼..也不要放置多行代碼將事情分開放在一行上,這使得很難閱讀你正在嘗試做的事情,並且很容易被查看或錯過這行'Console.WriteLine(「Age 1-> {0}'如果你使用'string.Format()'方法,它會清理我的意見 – MethodMan 2014-12-04 21:20:35
沒有理由改變字節... byte 0 -255,我不認爲很多人活到接近這個年齡。 – 2014-12-04 21:23:23