C#中的浮点/双型输入验证 [英] Float/Double type input validation in C#

查看:121
本文介绍了C#中的浮点/双型输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是我编写的第一个程序(本周一开始学习);我是一个新手.

This is literally my first program I've ever written (started learning this past Monday); I am a total newbie.

我的问题是,当程序提示用户输入华氏或摄氏温度(期望数字)时,如何防止当用户输入无效字符时引发异常????因此,例如,当用户输入"asfasd"时,程序将引发异常.

My question is, how can I prevent exceptions from being thrown when a user enters an invalid character when the program prompts the user for fahreinheit or celsius entry (expecting a number)??? So for example, when a user enters "asfasd", the program throws an exception.

发布此文章之前,我在网站上进行了很多搜索,并且能够成功找到其他输入验证问题,但是,这些问题都与C和C ++有关,并且由于我是新手,所以我很难了解这些语言以及它们与C#的关系.谢谢你.请查看代码:

I did a lot of searching on the site before posting this, and I was successfully able to find other input validation questions, however, they were all concerning C and C++ and since I am such a newbie, I have a hard time with understanding those languages and how they relate to C#. Thank you. Please see code:

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循环来验证用户输入.

You can either put it in Try-Catch block or use a while loop to validate the user input.

下面是带有while循环的代码,该代码可验证用户输入.

below is your code with a while loop which validates users input.

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;
    }
}

这篇关于C#中的浮点/双型输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆