下面的代码为什么在“是否购买(Y/n)”时要输入两次
不用线程就一次呢?本人刚刚学C# 请高手解答using System;
using System.Threading;
class p_n_c
{
    int n = 5; 
    public void producer()
    {
        
        int p_n ;
        Console.WriteLine("现有个数为:{0}",n);
        lock (this)
        {
            if (n < 10)
            {
                Console.WriteLine("Start produce");
                Console.WriteLine("输入生产个数:");
                p_n = Convert.ToInt32(Console.ReadLine());
                n = n + p_n;
                Console.WriteLine("现有个数为:{0}",n);
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("苍库中已经有10个以上的存货");
            }
        }
    }
    public void customer()
    {
        char acp;
        char acp1;
        int b_n;
        lock (this)
        {
            if (n > 0)
            {
                Console.WriteLine("现有个数为:{0}", n);
                for (; n >= 0; ) 
                {
                  
                    Console.WriteLine("是否购买?(Y/N)");                    
                    acp = Convert.ToChar(Console.ReadLine().ToLower());
                   
                    if (acp == 'y')
                    {
                        Console.WriteLine("输入购买个数:" );
                        b_n = Convert.ToInt32(Console.ReadLine());
                        n = n - b_n;
                        Console.WriteLine("剩下{0}个", n);
                        Console.WriteLine("继续购买吗?(Y/N)");
                        acp1 = Convert.ToChar(Console.ReadLine().ToLower());
                        if (acp1 == 'y') continue;
                        else break;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine("商品已经售完。");
            }
        }
    }   
}
class ThreadChild
{
    public static p_n_c pc = new p_n_c();
    public static void childthread1()
    {      
        pc.producer();
    }
    public static void childthread2()
    {
        pc.customer();
    }
    public static void Main()
    {        ThreadStart p1 = new ThreadStart(childthread1);
        ThreadStart c1 = new ThreadStart(childthread2);
        Thread threadc1 = new Thread(c1);
        Thread threadp1 = new Thread(p1);
        threadp1.Start();
        threadc1.Start();
        Console.ReadLine();
    }
}

解决方案 »

  1.   

    Console.WriteLine("Start produce"); 
                    Console.WriteLine("输入生产个数:"); 
                    p_n = Convert.ToInt32(Console.ReadLine()); 
                    n = n + p_n; 
                    Console.WriteLine("现有个数为:{0}",n); 
                    Console.ReadLine(); 
    你自己要输入2此啊。后一个Console。ReadLine();要等待输入。
      

  2.   

    在主函数加上 threadc1.join();
                threadp1.join();
    也可以