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

初学者使用C#验证数字和字母,c#,C#,我是C#新手,不知道如何在我已经编写的代码中进行验证。我的代码工作得很完美,但我想继续添加功能。我在找提示或任何你想提的东西。提前谢谢。 这是我到目前为止拥有的代码,需要在绿色注释附近的3个getInputs上进行验证。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BasicUserInterface { class Program

我是C#新手,不知道如何在我已经编写的代码中进行验证。我的代码工作得很完美,但我想继续添加功能。我在找提示或任何你想提的东西。提前谢谢。 这是我到目前为止拥有的代码,需要在绿色注释附近的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();
        }
    }
}
以下是验证字符串是否为数字的方法:

string str = "123";
int result;
if (!Int32.TryParse(str, out result))
    ; //not a whole number
TryParse
如果字符串未成功解析为有效整数,将返回false;如果成功解析,将在out参数中返回转换后的int

或者,如果您希望允许小数:

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


下面是如何计算字符串中非字母的字符数:

string str = "AB3C";
int numberOfNonLetters = str.Count(c => !Char.IsLetter(c));
要确保字符串只有字母,只需确保
numberOfNonLetters
为零即可

以下是验证字符串是否为数字的方法:

string str = "123";
int result;
if (!Int32.TryParse(str, out result))
    ; //not a whole number
TryParse
如果字符串未成功解析为有效整数,将返回false;如果成功解析,将在out参数中返回转换后的int

或者,如果您希望允许小数:

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


下面是如何计算字符串中非字母的字符数:

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

要确保字符串只有字母,只需确保
numberOfNonLetters
为零

数值类型有一个
TryParse
方法,可用于捕获非法输入

例如:

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.");
}

数值类型有一个
TryParse
方法,可用于捕获非法输入

例如:

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.");
}

你不是说特里帕斯吗?(也许你在看野马队的比赛)。@SteveWellens-我完全在看AFC的比赛,在一个2岁的孩子之后,所以是的,预计会有失误触地得分,你不是说TryParse吗?(也许你在看野马队的比赛)。@SteveWellens-我在看AFC的比赛,在一个2岁的孩子之后,我会注意到触地得分,是的,打字错误。谢谢你的帮助,我已经考虑了3个小时了。非常感谢。谢谢你的帮助,我已经考虑了3个小时了。非常感谢。