使用Visual Basic // C#
我正在尝试搜索我存储的数组以匹配用户输入.例如,用户已经存储了USB的数据,现在希望重新获得该信息.
以下完整代码
我已经使用IndexOf来查找数组索引,但现在我想搜索该索引以匹配用户的输入.这行代码:
if (ProductNameArray.Any(usersearch.Contains))
Run Code Online (Sandbox Code Playgroud)
我想出了那个错误
System.Array不包含"Any"的定义
但它在我的其他代码中有效.
我似乎无法弄清楚这一点,任何帮助表示赞赏.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
namespace Prac_Test3
{
class Program
{
// Constants
const int SIZE_OF_PRODUCT_CODE = 4;
const float CATEGORY_A_MARKUP = 10.0F;
const float CATEGORY_C_MARKUP = 33.3F;
const float CATEGORY_P_MARKUP = 15.0F;
const int ARRAY_SIZE = 100;
static void DisplayMenu()
{
Console.Clear();
Console.WriteLine("--------------> Menu <------------");
Console.WriteLine("1. Add a product (a)");
Console.WriteLine("2. Find a product (f)");
Console.WriteLine("3. Enter the quantity in stock (q)");
Console.WriteLine("4. Delete a product (d)");
Console.WriteLine("5. Calculate and display values (v)");
Console.WriteLine("6. Exit (x)");
Console.Write("\r\nEnter your selection: ");
}
static void AddProduct( string[] ProductNameArray, string[] ProductCodeArray, float[] WholesalePriceArray, ref int NextAvaliablePosition)
{
string ProductName = "";
string ProductCode = "";
string ProductCategory = "";
float WholesalePricePerItem = 0.0F;
bool ParseResult = false;
bool ErrorFlag = false;
string UserResponse = "";
do
{
ErrorFlag = false;
Console.Write("Product Name : ");
ProductName = Console.ReadLine();
if (ProductName == "")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Product Name must not be left blank.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
} while (ErrorFlag);
do
{
ErrorFlag = false;
Console.Write("Product Code : ");
ProductCode = Console.ReadLine();
if (ProductCode == "")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Product Name must not be left blank.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
else if (ProductCode.Length != SIZE_OF_PRODUCT_CODE)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Product Code must be exactly four characters.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
else
{
ProductCategory = ProductCode.Substring(0, 1);
ProductCategory = ProductCategory.ToUpper();
if (ProductCategory != "A" && ProductCategory != "C" && ProductCategory != "P")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Product Code must start with A, C or P.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
else if (!(Char.IsDigit(ProductCode[1])) && !(Char.IsDigit(ProductCode[2])) && !(Char.IsDigit(ProductCode[3])))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Product Code must be A, C or P followed by three digits.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
}
} while (ErrorFlag);
do
{
ErrorFlag = false;
Console.Write("Wholesale price per item ($): ");
UserResponse = Console.ReadLine();
ParseResult = float.TryParse(UserResponse, out WholesalePricePerItem);
if (ParseResult == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Not a valid number.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
else if (WholesalePricePerItem <= 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wholesale price must be a number greater than 0.");
Console.ForegroundColor = ConsoleColor.Gray;
ErrorFlag = true;
}
} while (ErrorFlag);
}
static void FindProduct(Array ProductNameArray)
{
int search = -1;
string usersearch;
usersearch = Console.ReadLine();
search = Array.IndexOf(ProductNameArray, usersearch);
if (search >=0)
{
if (ProductNameArray.Any(usersearch.Contains))
{
Console.WriteLine(" details blah blah");
}
}
else if (search <0)
{
Console.WriteLine("No record exists.");
}
Run Code Online (Sandbox Code Playgroud)
Cri*_*scu 18
你需要using System.Linq;这个才能工作.
Any 是LINQ中定义的扩展方法.
另外,要注意类型ProductNameArray.如果它被定义为Array(而不是string[],例如),编译器无法推断,枚举时,它将产生strings.
在这种情况下,你必须写:
if (ProductNameArray.Cast<string>().Any(usersearch.Contains))
Run Code Online (Sandbox Code Playgroud)
编辑:好的,看看代码似乎问题是上面描述的问题.
您必须更改FindProduct方法的签名
static void FindProduct(Array ProductNameArray)
Run Code Online (Sandbox Code Playgroud)
至
static void FindProduct(string[] ProductNameArray)
Run Code Online (Sandbox Code Playgroud)
或使用该.Cast<string>方法.
我个人更喜欢改变方法的签名,因为ProductNameArray传递给它似乎真的是一个string[].
| 归档时间: |
|
| 查看次数: |
12624 次 |
| 最近记录: |