需要解决这个问题

时间:2014-01-28 05:44:04

标签: c#

public class SelectedItems
{
    public Item item;
    public int quantity;
    public double subtotal = 0.0;

}

public class Item
{
    public int itemcode;
    public string itemname;
    public double unitprice = 0.0;
}

/* These are my classes */


class Program
{
    public static ArrayList details = new ArrayList();
    public static ArrayList purchased = new ArrayList();


    static void Main(string[] args)
    {
        Item i1 = new Item();
        i1.itemcode = 1111;
        i1.itemname = "Pants";
        i1.unitprice = 950.00;

        Item i2 = new Item();
        i2.itemcode = 2222;
        i2.itemname = "Dress";
        i2.unitprice = 850.00;

        Item i3 = new Item();
        i3.itemcode = 3333;
        i3.itemname = "Blouse";
        i3.unitprice = 650.00;

        Item i4 = new Item();
        i4.itemcode = 4444;
        i4.itemname = "Shirt";
        i4.unitprice = 1500.00;

        Item i5 = new Item();
        i5.itemcode = 5555;
        i5.itemname = "Belt";
        i5.unitprice = 1200.00; 


        details.Add(i1);
        details.Add(i2);
        details.Add(i3);
        details.Add(i4);
       // details.Add(i5);

       details.Add(new Item { itemcode = 5555, itemname = "Belt", unitprice = 1200.00 });

        /*Console.WriteLine("Enter the code :");
        searchItem(Convert.ToInt32(Console.ReadLine()));
        Console.ReadLine();*/

      Console.Write("Number Of Different Items:");
      int count = Convert.ToInt32(Console.ReadLine());

     while (count > 0)
     {  
        SelectedItems stm = new SelectedItems();
        Console.WriteLine("Enter the code :");
        Item im = searchItem(Convert.ToInt32(Console.ReadLine()));

        if (stm.item == null)

            Console.WriteLine("no item");

        else
        {
            Console.WriteLine("Enter the quantity :");
            stm.quantity = Convert.ToInt32(Console.Read());
            stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity));

            purchased.Add(stm);
        } 
        count--;
     }
     //printBill();
    }
    public static Item searchItem(int code)
    {
        foreach (Item i in details)
        {
            if (code == i.itemcode)
            {
                return i;
                Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice);
            }
        }
         return null;
    }

这总是不返回任何项目。我可以清楚地看到这个吗?我之前发布了这个并且能够正确使用它,现在它让人头疼了

7 个答案:

答案 0 :(得分:0)

尝试更改

if (stm.item == null)

Console.WriteLine("no item");

if (String.IsNullOrEmpty(stm.item))

    Console.WriteLine("no item");

答案 1 :(得分:0)

尝试将循环顶部更改为:

        SelectedItems stm = new SelectedItems();
        Console.WriteLine("Enter the code :");
        stm.item = searchItem(Convert.ToInt32(Console.ReadLine()));

看起来你没有分配stm.item属性。

答案 2 :(得分:0)

  

ArrayList属于C#没有泛型的日子。它的   不赞成使用List。你不应该在new中使用ArrayList   以.NET> = 2.0

为目标的代码

尝试使用

public static IList<Item> details = new List<Item>();

答案 3 :(得分:0)

因为你将它写入控制台,因为它上面的return语句而永远不会被执行。

将其更改为:

if (code == i.itemcode)
{
    Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice);
    return i;
}

并将结果分配给您正在检查null的对象:

Item stm = new Item ();
Console.WriteLine("Enter the code :");
stm= searchItem(Convert.ToInt32(Console.ReadLine()));

if (stm == null){//other code}

答案 4 :(得分:0)

变化

if (stm.item == null)
{
    Console.WriteLine("no item");
}

if (im == null)
{
    Console.WriteLine("no item");
}

有效!!

答案 5 :(得分:0)

您没有为属性 stm.item 分配任何值。因此它将始终为null并且如果(stm.item == null)将始终为真。

我不知道为什么你在那里使用 if(stm.item == null),但你必须做一些如下所示的事情。

stm.item = "Test";
if (stm.item == null)
    Console.WriteLine("no item");

searchItem 功能中存在一些问题

在return语句后没有语句执行。

return i;
Console.WriteLine("{0} : {1} : {2}", i.itemcode, i.itemname, i.unitprice);

答案 6 :(得分:0)

您将搜索项目返回到项目类的对象中,但不执行任何操作。然后,您对新创建的SelectItem进行比较,因为您尚未将项目添加到其中,因此当时为Null。尝试改变。

while (count > 0)
 {  
    SelectedItems stm = new SelectedItems();
    Console.WriteLine("Enter the code :");
    Item im = searchItem(Convert.ToInt32(Console.ReadLine())); //You are returning an item here and doing nothing
                                                               //with it. this is the only time you reference it
    if (stm.item == null)    //You are checking an empty(null) instance of your selectedItems class that you
                             // just created.
        Console.WriteLine("no item");

    else
    {
        Console.WriteLine("Enter the quantity :");
        stm.quantity = Convert.ToInt32(Console.Read());
        stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity));

        purchased.Add(stm);
    } 
    count--;
 }

while (count > 0)
 {  
    SelectedItems stm = new SelectedItems();
    Console.WriteLine("Enter the code :");
    Item im = searchItem(Convert.ToInt32(Console.ReadLine()));

    if (im == null)   //Check if searchItem returned an object.

        Console.WriteLine("no item");

    else
    {
        stm.Item = im;  // Added item to your selected items
        Console.WriteLine("Enter the quantity :");
        stm.quantity = Convert.ToInt32(Console.Read());
        stm.subtotal = stm.item.unitprice * (Convert.ToDouble(stm.quantity));

        purchased.Add(stm);
    } 
    count--;
 }