仅限用户输入数字

时间:2015-05-25 22:04:38

标签: c# validation

全新的C#[4小时新:)],但希望有一些关于Board Feet Calculator的指针,将用户输入限制为仅限数字,不允许使用字母或特殊字符。

首先,限制是在类,方法和/或程序中进行的吗? (我相信课程和方法)

其次,我在下面看到了一个例子,我会使用类似的东西吗?

第三,如果是这样,我是否需要为KeyPress和KeyPressEventArgs创建单独的类? (我相信他们自动在那里,例如 public char KeyChar { get; set; }

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // allows only letters
    if (!char.IsLetter(e.KeyChar))
    {
        e.Handled = true;
    }
}

我的计划

namespace BoardFt_MyTry_
{
    class Program
    {
        static void Main(string[] args)
        {
            Board board = new Board();

        board.lengthOfboard = Convert.ToDouble(askQuestion("What is the length of your board in inches?"));
        board.widthOfboard = Convert.ToDouble(askQuestion("What is the width of your board in inches?"));
        board.thicknessOfboard = Convert.ToDouble(askQuestion("What is the thickness of your board in inches?"));

        Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());

        Console.ReadLine();
    }
    private static string askQuestion(string question)
    {
        Console.WriteLine(question);
        return Console.ReadLine();
    }

}

我的董事会成员

namespace BoardFt_MyTry_
{
    class Board
    {
        public double lengthOfboard;
        public double widthOfboard;
        public double thicknessOfboard;

    public double CalcBoardFt()
    {
        double boardft = 0;

        boardft = (this.lengthOfboard * this.widthOfboard * this.thicknessOfboard) / 144;

        return boardft;
    }
}
}

3 个答案:

答案 0 :(得分:5)

您无法在控制台应用程序中真正做到这一点。您所能做的就是允许用户输入错误数据,然后告诉用户数据是坏的。

您可以尝试这样的事情:

class Program
{
    public double AskDnoubleQuestion(string message){
        do {
        Console.Write(message);
        var input = Console.ReadLine();

        if (String.IsNullOrEmpty(input)){
            Console.WriteLine("Input is required");
            continue;
         }
         double result;
         if (!double.TryParse(input, out result)){
           Console.WriteLine("Invalid input - must be a valid double");
           continue;
         }
         return result;
    }

    static void Main(string[] args)
    {
        Board board = new Board();

    board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?");
    board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?");
    board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?");

    Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt());

    Console.ReadLine();
}

答案 1 :(得分:1)

如果验证不是你想要的方式,你可以这样做:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a number:");

        string number = ReadNumber();
        Console.WriteLine("You entered: " + number);
    }

    private static string ReadNumber()
    {
        string input = "";

        do
        {
            ConsoleKeyInfo keyInfo = Console.ReadKey(true);
            if (char.IsNumber(keyInfo.KeyChar))
            {
                input = input + keyInfo.KeyChar;
                Console.Write(keyInfo.KeyChar);
            }
            if (keyInfo.Key == ConsoleKey.Enter)
            {
                Console.WriteLine();
                break;
            }
            if (keyInfo.Key == ConsoleKey.Backspace)
            {
                input = input.Substring(0, input.Length - 1);
                Console.Write("\b \b");
            }
        } while (true);

        return input;
    }
}

这将允许用户仅输入数字。如果你愿意,你可以随意过滤它。例如,只有字母和数字等......

现在看来,它只允许整数。如果要允许小数点,请将上面的行更改为:if (char.IsNumber(keyInfo.KeyChar) || keyInfo.KeyChar == '.')

答案 2 :(得分:0)

您可以编写一个按键读取的方法(不在控制台中显示),忽略非数字字符,打印有效字符并将它们附加到StringBuilder实例,如这样:

public static string ReadOnlyNumbers()
{
    StringBuilder input = new StringBuilder();
    ConsoleKeyInfo ckey;

    while ((ckey = Console.ReadKey(true)).Key != ConsoleKey.Enter)
    {
        if (Char.IsDigit(ckey.KeyChar))
        {
            Console.Write(ckey.KeyChar);
            input.Append(ckey.KeyChar);
        }

        if (ckey.Key == ConsoleKey.Backspace)
        {
            input.Length--;
            Console.Write("\b \b");
        }
    }

    Console.Write(Environment.NewLine);
    return input.ToString();
}

然后您可以像这样使用它:

string input = ReadOnlyNumbers();
Console.WriteLine(input);