[英]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.