如何使程序仅接受用户输入的整数值

时间:2019-06-14 10:27:01

标签: c# user-input

我已经创建了将一个月中的天数返回到控制台的代码,我想知道是否有一种方法可以验证用户输入是否为整数,如果不是整数则返回一条消息说“错误的用户输入必须是整数”

这是我现在创建的代码:

 using System;

 public static class GlobalMembers
{
 static int Main()
 {

    {


        Console.Write("Enter a Date to obtain how many days are in a month:");
        Console.Write("\n");
        int year = 0;
        int month = 0;
        int days = 0;
        if (month < 1 || month > 12) 
            Console.Write("Enter Year: ");
        year = Convert.ToInt32(Console.ReadLine());


        Console.Write("Enter Month Number: ");
        month = Convert.ToInt32(Console.ReadLine());


        if (month == 4 )
        {

             Console.Write("there are 30 days in the month April");
            Console.ReadLine();

        }

        if (month == 6)
        {
            Console.Write("there are 30 days in the month June");
            Console.ReadLine();


        }

        if (month == 9)
        {
            Console.Write("there are 30 days in the month September");
            Console.ReadLine();

        }


        if (month == 11)
        {
            Console.Write("there are 30 days in the month November");
            Console.ReadLine();

        }

        if (month == 1)
        {
            Console.Write("there are 31 days in the month January");
            Console.ReadLine();

        }


        if (month == 3)
        {
            Console.Write("there are 31 days in the month March");
            Console.ReadLine();

        }


        if (month == 5)
        {
            Console.Write("there are 31 days in the month May");
            Console.ReadLine();

        }


        if (month == 7)
        {

            Console.Write("there are 31 days in the month july");
            Console.ReadLine();
        }



        if (month == 8)
        {
            days = 31;
            Console.Write("there are 31 days in the month August");
            Console.ReadLine();

        }


        if (month == 10)
        {
            days = 31;
            Console.Write("there are 31 days in the month October");
            Console.ReadLine();

        }


        if (month == 12)
        {

            Console.Write("there are 31 days in the month December");
                            Console.ReadLine();

        }

        if (month < 1 || month > 12)

        {
            Console.Write("Error month range should be between 1-12");
            Console.ReadLine();
        }



        else if (month == 2)
        {
            bool leapyear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

            if (leapyear == false)
            {
                days = 28;
                Console.Write("there are 28 days in the month Febuary");
                Console.ReadLine();

            }
            else
            {
                days = 29;
                Console.Write("there are 29 days in the month Febuary due to leap year");
                Console.ReadLine();



            }
        }

        else 
        {
            days = 31; 


        }



        return days;
        }
    }
 }

非常感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这仍然允许用户输入其他字符, 但它只接受数字:

bool correctInput = false;
while (!correctInput) // this loop will continue until the user enters a number
{
    Console.Write("Enter Year: ");
    correctInput = int.TryParse(Console.ReadLine(), out year); // if the parsing was successful it returns true

    if (!correctInput)
    {
        Console.Write("Your input was not a number");
    }
}

答案 1 :(得分:0)

您可以在文本框按键事件中阻止用户使用此代码输入字符串或其他字符:

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
 {
 e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
 }

此外,您还需要阻止ctrl + v之类的快捷方式,因为用户可以将字符串粘贴到文本框中。如果要执行此操作,请单击文本框并转到属性,然后将快捷方式属性更改为禁用。