簡體   English   中英

C#十進制用戶輸入和響應

[英]C# Decimal user input with response

首先,我想說我對C#還是很陌生,但是我對C#的了解還不是很豐富,但是我在不同的站點上瀏覽,卻沒有找到解決方案(或者也許有,但是我失敗了),無論如何,我正在嘗試實現一個功能,其中用戶鍵入他的GPA得分(例如4.5),並且如果它包含字母或其他字符(例如4.5a $ g),它將返回以詢問數字。 這是我目前擁有的:

Console.WriteLine("\nGPA Score: ");
userinput2 = Console.ReadLine();
int Result = 0;
bool TGPA = int.TryParse(userinput2, out Result);

if (TGPA)
{
     Console.WriteLine(Result);
}
else
{
     Console.WriteLine("Please enter a numerical value.");
}

請編寫循環,直到獲得有效數字(在這種情況下為小數)。

        Console.WriteLine("\nGPA Score: ");
        string userinput2 = Console.ReadLine();
        decimal Result = 0;

        bool TGPA = decimal.TryParse(userinput2, out Result);

        while (TGPA == false)
        {
            Console.WriteLine("{0} is not a valid number, please try again", userinput2);
            userinput2 = Console.ReadLine();
            Result = 0;
            TGPA = decimal.TryParse(userinput2, out Result);
        }

(例如4.5)

那不是整數。 嘗試十進制:

decimal Result = 0M;
bool TGPA = decimal.TryParse(userinput2, out Result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM