gpt4 book ai didi

c# - Windows Phone 文本字段中的小数点

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:32 25 4
gpt4 key购买 nike

您好 C# 和 Windows 手机开发人员,

对于我的 Windows Phone 应用程序,我有一个文本字段,要求用户输入他们的年龄。在 Debug模式下,我输入了数字 .8。然后单击继续,应用程序意外关闭。我需要添加什么代码才能发布一个消息框,通知用户小数点后一位以上的数字是 Not Acceptable 。请帮忙

最佳答案

假设输入是一个字符串,试试:

if (input.IndexOf('.') == -1 || input.LastIndexOf('.') == input.IndexOf('.'))
{
//good
}
else
MessageBox.Show("More than one decimal point");

更好的方法是使用 TryParse,它会检查数字的格式

float age;
if (float.TryParse(input, out age))
{
//good
}
else
MessageBox.Show("Invalid age.");

关于c# - Windows Phone 文本字段中的小数点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com