English 中文(简体)
C#:试图制造鞋店:试图评估正确的SKU
原标题:C#: Attempting to create a shoe store: trying to evaluate the correct SKU

I m试图建立一个鞋店方案,用户将在SKU号中打上鞋子。 如果SKUs匹配或显示“有效SKU:如果无效,我就试图制造这样的说法。 现在,这只是说,无论SKU I说什么,数字都是无效的。 当入境有效时,不再要求数量!

while (keepBuyingShoes == true)
{
    // ask user for sku and quantity 
    int quantity = 0;
    Console.WriteLine("Enter shoe SKU: ");
    String sku = Console.ReadLine();
    if (sku != s1.SKU || sku != s2.SKU || sku != s3.SKU || sku != s4.SKU || sku != s5.SKU)
    {
       Console.WriteLine("That is an invalid SKU number!");
       Console.WriteLine("Enter shoe SKU: ");
       sku = Console.ReadLine();
    }
    else if (sku == s1.SKU || sku == s2.SKU || sku == s3.SKU || sku == s4.SKU || sku == s5.SKU)
    {
      Console.WriteLine("Enter quantity: ");
      quantity = int.Parse(Console.ReadLine());
    }
问题回答

您是否注意到您在“<<>条/代码>上都使用了<>条码>,尽管究竟是谁应该对面? 是否奇怪? 至少应使用<代码>&& 在这种情况下? Huh。

因此,正如你可能认为的那样,你的状况是错误的:

if (sku != s1.SKU || sku != s2.SKU || sku != s3.SKU || sku != s4.SKU || sku != s5.SKU)
{
    // ...
}

如果你实际上想到逻辑的话,这是显而易见的:你基本上说“如果SKU isn t s1。 页: 1 页: 1 页: 1 页: 1 为了伪造,SKU将不得不一劳永逸地打到<>所有>,这是不可能做到的。 因此,它是一种美学。 并不奇怪的是,它总是输入if

解决办法是使用<代码>&&。 相反:

if (sku != s1.SKU && sku != s2.SKU && sku != s3.SKU && sku != s4.SKU && sku != s5.SKU)
{
    // ...        
}

这样,如果SKU从来不是其中任何一个,那么你就进入<条码>。

如果,则你只能将以下<代码>e>改为“条码>。

Now, you should probably shove all those SKU objects into a collection and use things like Any and All, but that s a topic for another time.

Having multiple variables like s1 , s2 etc will not scale well. Everytime you add a new item like s101 you are going to need to update your logic, which is not ideal.

更不用像一个阵列或名单那样收集。 然后,你可以通过林克询问收集情况。

类似:

var skuList = new List<YOUR_SKU_TYPE>() {
    new YOUR_SKU_TYPE() {SKU = "SKU1" } ,
    new YOUR_SKU_TYPE() {SKU = "SKU2" } ,
    new YOUR_SKU_TYPE() {SKU = "SKU3" }
};

while (keepBuyingShoes == true)
{
    // ask user for sku and quantity 
    int quantity = 0;
    Console.WriteLine("Enter shoe SKU: ");
    String sku = Console.ReadLine();

    //Check if sku matches one in our list
    if (skuList.Exists(x => x.SKU == sku)
    {
      Console.WriteLine("Enter quantity: ");
      quantity = int.Parse(Console.ReadLine());
    }
    else
    {
       Console.WriteLine("That is an invalid SKU number!");
       Console.WriteLine("Enter shoe SKU: ");
       sku = Console.ReadLine();
    }
}

<>NOTE 代码完全未经测试,可能需要一些 de。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签