我對C#和編程一般都很陌生,我花了幾個小時試圖修復我的基本程序。我有4個變量,我要求用戶輸入3個變量,並留下一個空的變量。該程序對空白進行計算,但問題是我無法使用IsNullOrWhiteSpace作爲整數。是否有整數的string.IsNullOrWhiteSpace版本?
Console.WriteLine("ilk aracın hızını giriniz");
int v1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("ikinci aracın hızını giriniz");
int v2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("yolun uzunluğunu giriniz");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("karşılaşma sürelerini giriniz");
int t = Convert.ToInt32(Console.ReadLine());
if (string.IsNullOrWhiteSpace(v1))
{
v1 = x/t - v2;
Console.WriteLine(v1);
}
else if (string.IsNullOrWhiteSpace(v2))
{
v2 = x/t - v1;
Console.WriteLine(v2);
}
else if (string.IsNullOrWhiteSpace(t))
{
t = x/(v1 + v2);
Console.WriteLine(t);
}
else if (string.IsNullOrWhiteSpace(x))
{
x = (v1 + v2) * t;
Console.WriteLine(x);
}
` 有沒有辦法解決這個問題?如果是這樣如何?
可空INT''INT嘗試 –
雙N;? bool isNumeric = Int.TryParse(「5」,out n); – Sujatha
爲什麼不對源字符串執行'IsNullOrWhitespace()'而不是立即(並且沒有任何驗證)將它們轉換爲'int'? –