简体   繁体   English

初学者使用C#验证数字和字母

[英]Validation of numbers and letters using C# for a beginner

I'm new to C# and can't figure out how to validate within the code I already wrote. 我是C#的新手,无法弄清楚如何在已经编写的代码中进行验证。 I have the code working perfect, but want to keep adding features. 我的代码可以完美运行,但是想要继续添加功能。 I'm looking for tips or anything you care to mention. 我正在寻找提示或您想提的任何东西。 Thanks ahead of time. 提前谢谢。 Here is the code I have so far and need validation on the 3 getInputs near the green comments. 这是我到目前为止的代码,需要对绿色注释附近的3个getInputs进行验证。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BasicUserInterface
{
    class Program
    {
        static void DisplayApplicationInformation()
        {
            Console.WriteLine("Welcome to the Basic User Interface Program");
            Console.WriteLine("CIS247, Week 2 Lab");
            Console.WriteLine("Name: Fred Ziyad");
            Console.WriteLine();
            Console.WriteLine("This program accepts user input as a string, then makes the");
            Console.WriteLine("approppriate data conversion and display the results.");
            Console.WriteLine();
        }

        static void DisplayDivider(String outputTitle)
        {
            Console.WriteLine("************* " + outputTitle + " **************");
        }

        static string GetInput(string inputType)
        {
            string strInput;

            Console.Write("Enter " + inputType + ": ");
            strInput = Console.ReadLine();

            return strInput;
        }

        static void TerminateApplication()
        {
            Console.WriteLine();
            Console.WriteLine("Thank you for using the Basic User Interface program");
            Console.Read();
        }

        static void Main(string[] args)
        {
            int age;
            double mileage;
            string strInput, name;

            DisplayApplicationInformation();

            DisplayDivider("Start Program");
            Console.WriteLine();

            DisplayDivider("Get Name");
            name = GetInput("your name");
            Console.WriteLine("Your name is " + name);
            Console.WriteLine();
            //Validate name to be a string of letters.

            DisplayDivider("Get Age");
            strInput = GetInput("your age");
            age = int.Parse(strInput);
            Console.WriteLine("Your age is: " + age);
            Console.WriteLine();
            //Validate age to be a number.

            DisplayDivider("Get Mileage");
            strInput = GetInput("gas mileage");
            mileage = double.Parse(strInput);
            Console.WriteLine("Your car MPT is: " + mileage);
            //Validate mileage to be a number.

            TerminateApplication();
        }
    }
}

The numeric types have a TryParse method that you can use to catch illegal input. 数值类型具有TryParse方法,可用于捕获非法输入。

Example: 例:

DisplayDivider("Get Age");
strInput = GetInput("your age");
if (int.TryParse(strInput, out age)) {
  Console.WriteLine("Your age is: " + age);
  Console.WriteLine();
} else {
  Console.WriteLine("Age input was not a valid number.");
}
//Validate age to be a number.

Here's how you validate whether a string is a number: 这是验证字符串是否为数字的方法:

string str = "123";
int result;
if (!Int32.TryParse(str, out result))
    ; //not a whole number

TryParse will return false if the string doesn't successfully parse out as a valid integer, and, if it does parse successfully, will return the converted int in the out parameter. TryParse将返回false,如果字符串无法成功解析出一个有效的整数,并且,如果成功的解析,将在输出参数返回转换INT。

Or if you want to allow decimals: 或者,如果您想允许小数:

string str = "123.5";
double result;
if (!Double.TryParse(str, out result))
    ; //not a number

Same idea 相同的想法


//Validate name to be a string of letters.

Here's how you count the number of characters in a string that are not letters : 这是计算字符串中非字母的字符数的方法

string str = "AB3C";
int numberOfNonLetters = str.Count(c => !Char.IsLetter(c));

To make sure a string has only letters, just make sure numberOfNonLetters is zero 要确保字符串仅包含字母,只需确保numberOfNonLetters为零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM