-5

这就是我到目前为止所做的

     Console.Write("Enter your post code >");
        post_code = Console.ReadLine();
        if (string.IsNullOrEmpty(post_code))
        {
            Console.WriteLine("Please enter correct data");
            return;
        }

        else if (post_code.Length != 4)
        {
            Console.WriteLine("Please enter correct data");
            return;
        }

我需要: - 第一个数字不能是 1、8 或 9。 - 第一个数字必须与下表中的状态相匹配:

州:新台币| 新南威尔士州| 维克| 昆士兰| 南非| 西澳| 助教|

第一个数字:0 | 2 | 3 | 4 | 5 | 6 | 7 |

4

4 回答 4

1

正则表达式可用于验证邮政编码:

    Regex postCodeValidation = new Regex(@"^[0234567]\d{4}$");
    if (postCodeValidation.Match(post_code).Success)
    {
        // Post code is valid
    }
    else
    {
        // Post code is invlid
    }

请注意:在上面的代码中,邮政编码被认为是 5 位数字(要更改长度 - 您需要用适当的数字4替换正则表达式模式)。 [0234567]\d{4}

于 2012-08-10T09:52:27.797 回答
1

使用正则表达式:

using System;
using System.Text.RegularExpressions;

namespace PostCodeValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            var regex = new Regex(@"^[0234567]{1}\d{3}$");
            var input = String.Empty;

            while (input != "exit")
            {
                input = Console.ReadLine();
                Console.WriteLine(regex.IsMatch(input));
            }
        }
    }
}

非正则表达式解决方案:

    static bool ValidPostCode(string code)
    {
        if (code == null || code.Length != 4)
        {
            return false;
        }
        var characters = code.ToCharArray();
        if (characters.Any(character => !Char.IsNumber(character)))
        {
            return false;
        }
        if ("189".Contains(characters.First()))
        {
            return false;
        }
        return true;
    }

另一个,没有 LINQ:

    static bool SimpleValidPostCode(string code)
    {
        if (code == null || code.Length != 4)
        {
            return false;
        }
        if ("189".Contains(code[0]))
        {
            return false;
        }
        for (var i = 1; i < 4; i++)
        {
            if (!"123456789".Contains(code[i]))
            {
                return false;
            }
        }
        return true;
    }

I only can do it with : if, if … else, if … else if … else constructs Nested ifs CASE and switch constructs

如果for循环也不在您允许的语言结构列表中,您仍然可以尝试:

    static bool SimpleValidPostCode(string code)
    {
        if (code == null || code.Length != 4)
        {
            return false;
        }
        if (code[0] == '1') return false;
        if (code[0] == '8') return false;
        if (code[0] == '9') return false;

        return "0123456789".Contains(code[1]) && "0123456789".Contains(code[2]) && "0123456789".Contains(code[3]);            
    }
于 2012-08-10T09:57:21.750 回答
1

我不确定状态是否是您的四字符邮政编码的一部分也包含状态。但如果没有,你可以这样做:

Dictionary<string,string> statePostCodeMap = new Dictionary<string,string>();

// Populate the dictionary with state codes as key and post code first character as value

if(!post_code.StartsWith(statePostCodeMap([NameOfVariableThatHoldsState])){
// Incorrect post code
}

编辑:根据用户评论:

这就是你可以使用的:

if !((string.Compare(state, "NT", true) == 0 && post_code.StartsWith("0"))){
// Incorrect Data
}
else if(<similar condition for other values>)
...

我认为这是某种学习练习。

于 2012-08-10T09:55:15.137 回答
1

看看正则表达式:http: //msdn.microsoft.com/fr-fr/library/system.text.regularexpressions.regex.aspx这就是你需要的:)

于 2012-08-10T09:46:50.813 回答