C# C语言中的浮点/双类型输入验证#
这是我写的第一个程序(上周一开始学习);我完全是个新手 我的问题是,当程序提示用户输入华氏温度或摄氏温度(需要数字)时,当用户输入无效字符时,如何防止引发异常???例如,当用户输入“asfasd”时,程序抛出一个异常 我在发布之前在网站上做了很多搜索,我成功地找到了其他输入验证问题,但是它们都是关于C和C++的,因为我是个新手,我很难理解这些语言以及它们是如何与C语言相关的。非常感谢。请参阅代码:C# C语言中的浮点/双类型输入验证#,c#,validation,input,C#,Validation,Input,这是我写的第一个程序(上周一开始学习);我完全是个新手 我的问题是,当程序提示用户输入华氏温度或摄氏温度(需要数字)时,当用户输入无效字符时,如何防止引发异常???例如,当用户输入“asfasd”时,程序抛出一个异常 我在发布之前在网站上做了很多搜索,我成功地找到了其他输入验证问题,但是它们都是关于C和C++的,因为我是个新手,我很难理解这些语言以及它们是如何与C语言相关的。非常感谢。请参阅代码: using System; namespace Converter { class Progr
using System;
namespace Converter
{
class Program
{
static void Main()
{
float? FahrenheitInput = null;
double? CelsiusInput = null;
float? KilogramInput = null;
float? PoundsInput = null;
int UserChoice = 0;
do
{
Console.WriteLine("What would you like to convert? Enter the corresponding number.\n1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit\n3. Pounds to Kilograms\n4. Kilograms to pounds\n5. Exit program");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
Console.WriteLine("Enter the temperature in Fahreinheit, number only:");
FahrenheitInput = float.Parse(Console.ReadLine());
Console.Clear();
Console.WriteLine(FahrenheitInput + " degrees fahrenheit in Celsius is " + Program.FahrenheitToCelsius(FahrenheitInput) + "\n\n");
break;
case 2:
Console.WriteLine("Enter the temperature in Celsius, number only:");
CelsiusInput = double.Parse(Console.ReadLine());
Console.Clear();
Console.WriteLine(CelsiusInput + " degrees Celius in fahrenheit is " + Program.CelsiusToFahrenheit(CelsiusInput) + "\n\n");
break;
case 5:
break;
default:
Console.WriteLine("This is not a valid entry. Please enter 1, 2, 3, 4, or 5.");
break;
}
} while (UserChoice != 5);
}
public static float? FahrenheitToCelsius(float? INPUT)
{
return (INPUT - 32) * 5 / 9;
}
public static double? CelsiusToFahrenheit(double? INPUT)
{
return INPUT * 1.8 + 32;
}
}
}用
int.TryParse
代替int.Parse
和float.TryParse
代替float。Parse
TryParse
是你在这里的好朋友。在大多数情况下,您应该喜欢使用TryParse
而不是Parse
。在您的示例中,您可以执行以下操作:
int validInt;
int.TryParse(Console.ReadLine(), out validInt);
float validFloat;
float.TryParse(Console.ReadLine(), out validFloat);
IMHO,更改例程的最简单方法是将
Parse
重写为相应的TryParse
:
// UserChoice = int.Parse(Console.ReadLine());
UserChoice = int.TryParse(Console.ReadLine(), out UserChoice) ? UserChoice : -1;
稍微复杂一点(您必须将float
转换为float?
)
CelsiusInput的相同方案
// CelsiusInput = double.Parse(Console.ReadLine());
double d;
CelsiusInput = double.TryParse(Console.ReadLine(), out v) d (double?) d : null;
代码的基本机制是
- 我们尝试解析用户输入
TryParse(Console.ReadLine()…
- 如果解析成功(因此
TryParse
返回true
),我们只返回out(解析值)
- 如果解析失败(因此
TryParse
返回false
),我们将返回一些特殊的值(-1
用于UserChoice
或FahrenheitInput
情况下的null
)
p.S.在第一个开关中,您只有案例1
、案例2
和案例5
;但是,您已经输入了“这不是一个有效的条目。请输入1、2、3、4、或5。”在错误消息中。似乎您必须在开关中执行案例3和案例4
,或者编辑错误消息。您可以将其放入Try-Catch块或使用while循环来验证用户输入
下面是带有while循环的代码,该循环验证用户输入
class Program
{
static void Main(string[] args)
{
double FahrenheitInput = 0;
double CelsiusInput = 0;
double KilogramInput = 0;
double PoundsInput = 0;
int UserChoice = 0;
do
{
Console.WriteLine("What would you like to convert? Enter the corresponding number.\n1. Fahrenheit to Celsius");
Console.WriteLine("2. Celsius to Fahrenheit\n3. Pounds to Kilograms\n4. Kilograms to pounds\n5. Exit program");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
Console.WriteLine("Enter the temperature in Fahreinheit, number only:");
while (!double.TryParse(Console.ReadLine(), out FahrenheitInput))
{
Console.WriteLine("Invalid format, please input again!");
};
Console.Clear();
Console.WriteLine(FahrenheitInput + " degrees fahrenheit in Celsius is " + Program.FahrenheitToCelsius(FahrenheitInput) + "\n\n");
break;
case 2:
Console.WriteLine("Enter the temperature in Celsius, number only:");
while (!double.TryParse(Console.ReadLine(), out CelsiusInput))
{
Console.WriteLine("Invalid format, please input again!");
};
Console.Clear();
Console.WriteLine(CelsiusInput + " degrees Celius in fahrenheit is " + Program.CelsiusToFahrenheit(CelsiusInput) + "\n\n");
break;
case 5:
break;
default:
Console.WriteLine("This is not a valid entry. Please enter 1, 2, 3, 4, or 5.");
break;
}
} while (UserChoice != 5);
}
public static double FahrenheitToCelsius(double INPUT)
{
return (INPUT - 32) * 5 / 9;
}
public static double CelsiusToFahrenheit(double INPUT)
{
return INPUT * 1.8 + 32;
}
}
虽然提供的所有答案似乎都有效,但您提出的问题是
如何防止抛出异常[…]
我想指出的是,你可以把抛出异常的部分放到一个try
-catch
-块中,这样做是在try
中执行代码,直到抛出一个异常,然后将这个异常作为参数传递给catch代码>-您可以处理它的部分:
示例
do
{
try
{
// the code you already have
}
catch (Exception ex)
{
Console.WriteLine("This is no valid input (" + ex.Message + ")! Try again...");
}
} while (UserChoice != 5);
当然,从一开始就防止异常发生是更好的方法(正如所有其他答案所建议的),但这种方法同样有效,并且更通用,以防将来遇到类似问题。使用开关-case
-语句进行错误处理是非常常见的做法…您正在寻找try-catch或regex,但当您本周开始阅读try-catch文章时
do
{
try
{
// the code you already have
}
catch (Exception ex)
{
Console.WriteLine("This is no valid input (" + ex.Message + ")! Try again...");
}
} while (UserChoice != 5);