简体   繁体   English

如何将数组的每个部分通过测试以返回输出?

[英]How do I put each part of my array through a test to return an output?

I'm trying to get the testData to be checked by the NHS number checker code below. 我正在尝试通过下面的NHS号码检查器代码检查testData。 How do I test each part of the array & get the output? 如何测试数组的每个部分并获取输出?

 using System;
public class Program
{
public static void Main()
{
    {
        //Test loop


        string[] testData = { "1234567890", "4444444444", "7777777777", "77777777", "BRADLEYPAU" };
        foreach (string s in testData)
        {

            Console.WriteLine();
        }
    }



    Boolean returnValue;
    String NHSNumber;
    //NHSNumber = "9051292074";
    NHSNumber = "7777777777";
    //NHSNumber = "9434765919";
    //NHSNumber = "PAULBRADLE";
    returnValue = isNHSValid(NHSNumber);

    if (returnValue == true)
    {
        Console.WriteLine(NHSNumber + " looks good");
    }
    else
    {
        Console.WriteLine(NHSNumber + " is invalid!");
    }
}
public static Boolean isNHSValid(string numberToCheck)
{
    if (numberToCheck.Length != 10)
    {
        Console.WriteLine(numberToCheck + " is too long!");
        return false;
    }

    long v;
    if (Int64.TryParse(numberToCheck, out v) == false)
    {
        Console.WriteLine(numberToCheck + " contains non numbers!");
        return false;
    }

I have tried other examples that are commented out and they work. 我尝试了其他示例,这些示例已被注释掉并且它们起作用。 But now I can't figure out how to do the same thing with an array. 但是现在我不知道如何对数组执行相同的操作。

Like mentioned in the comments , use the loop you created to iterate over each string in the array. 注释中所述,请使用创建的循环遍历数组中的每个string Then pass each string into isNHSValid . 然后将每个string传递给isNHSValid

   string[] testData = { "1234567890", "4444444444", "7777777777", "77777777", "BRADLEYPAU" };
   foreach (string NHSNumber in testData)
   {
       bool returnValue;
       returnValue = isNHSValid(NHSNumber);

       if (returnValue == true)
       {
           Console.WriteLine(NHSNumber + " looks good");
       }
       else
       {
           Console.WriteLine(NHSNumber + " is invalid!");
       }
       Console.WriteLine();
   }

Ideally, you could make the tests into a unit test project and call your validation code. 理想情况下,您可以将测试放入单元测试项目中,然后调用验证代码。

For now, you can move you testing code into the for loop (or another function). 现在,您可以将测试代码移至for循环(或其他函数)中。 (It will simplify slightly too): (它也会略微简化):

public static void Main()
{
    //Test loop

    string[] testData = { "1234567890", "4444444444", "7777777777", "77777777", "BRADLEYPAU" };
    foreach (string NHSNumber in testData)
    {
        if (isNHSValid(NHSNumber)) //note, no need compare with true
        {
            Console.WriteLine(NHSNumber + " looks good");
        }
        else
        {
            Console.WriteLine(NHSNumber + " is invalid!");
        }
        Console.WriteLine();
    }

}

暂无
暂无

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

相关问题 C#如何写入数组的每个部分? - C# How do I write to each part of my array? 我在哪里放一个数组和一个函数,我将通过我的所有程序c#使用? - Where do I put an array and a function that I will use through all my program c#? 如何将数字1-10随机排列到数组中,然后一次调用数组的每个部分? - How do I order the digits 1-10 randomly into an array and then call each part of the array one at a time? 为什么当我尝试返回我的数组时有 System.Int32[] 作为输出? - why do I have System.Int32[] as output when I try to return my array? 如何将一个数组的每个部分分成另一个数组? - how do you separate each part of an array into another array? 如何在代码的这一部分放置一个变量? - How can I put a variable in this part of my code? 如何通过使用CORS的WebAPI控制器将来自其他域的值放入模型中? - How do I put a value from a different domain in my Model through a WebAPI Controller using CORS? 如何通过字典进行迭代 <MyEnum, List<int> &gt;并且每次返回每个列表的值的一部分? - How to iterate through Dictionary<MyEnum, List<int>> and each time return part of values of each list? 如何在C#中遍历数组的内容,然后仍返回整个数组? - How do I iterate through the contents of an array in C# and still return the entire array afterward? 我如何接收输入到C#中的二维数组中的输入? - How do i Receive input which I put into my two dimensional array in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM