我的问题是,当程序提示用户输入fahreinheit或celsius条目(期待一个数字)时,如何防止用户输入无效字符时抛出异常?因此,例如,当用户输入“asfasd”时,程序会抛出异常.
在发布之前我在网站上做了很多搜索,我成功地找到了其他输入验证问题,然而,他们都是关于C和C的,因为我是一个新手,我很难理解那些语言以及它们与C#的关系.谢谢.请看代码:
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; }}
}
解决方法 您可以将其放在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,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,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; }}总结
以上是内存溢出为你收集整理的C#中的Float / Double类型输入验证全部内容,希望文章能够帮你解决C#中的Float / Double类型输入验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)