class Parking
{
   boolean occupe[];
   int nbplace;
   int i=0;
  public Parking(int nbp)
  {   int nbplace=nbp;
      occupe=new boolean[nbplace];
      for(int i=0;i<occupe.length;i++)  
          occupe[i]=false;
   }
   public  synchronized int bookPlace(){          
        for(;i<nbplace;i++)
            if(!occupe[i]){  
                
                occupe[i]=true;//occupid
                
             }
           
          return i;
    }
}
class Client extends Thread{
  private final static Parking v=new Parking(10);
   
     public Client(String name){
                super(name);
                
                
               start();
     }
 
    public  void run(){
     System.out.println(getName()+"预定了第"+v.bookPlace()+"号车位!\n");
     }
    public static void main(String s[]){
                new Client("dog");
                new Client("cat");
                new Client("mouse");
    }}
  
模拟三个用户 new Client("dog");  来预定停车位
                new Client("cat");
                new Client("mouse");
为什么结果是:dog预定了第0号车位!        cat预定了第0号车位!        mouse预定了第0号车位!
应该是不同的号码,应怎样改啊????

解决方案 »

  1.   

    Parking类的构造方法参数传递的是停车场的车位个数
      

  2.   

    if(!occupe[i]){  
                    
                    occupe[i]=true;//occupid
                    
                 }
               
              return i;可以改为
     if(!occupe[i]){  
                    
                    occupe[i]=true;//occupid
                     return i;
                    
                 }
               
             
      

  3.   

    1:          if(!occupe[i]){  
                    
                    occupe[i]=true;//occupid
                    
                 }
               
              return i;////////
    2:      if(!occupe[i]){  
                    
                    occupe[i]=true;//occupid
                     return i;
                    
              }
    我认为这两种形式结果都是一样的,因为 i是 Parking类的成员变量  
    若用第二种形式的话,编译通不过,提示要求返回值!!
    这是为什么???????????????
      

  4.   

    class Client extends Thread{
      private final static Parking v=new Parking(10);
      ..... 
        public  void run(){
         System.out.println(getName()+"预定了第"+v.bookPlace()+"号车位!\n");
    //v是一个静态变量,而Thread.run()是一个非静态方法,为什么不报错???????
         }
        .....
      

  5.   

    因为你放在for循环里面的话,i++没有几乎执行呀!
    当然每次都返回0
      

  6.   

    class Parking
    {
       boolean occupe[];
       int nbplace;
       int i=0;
      public Parking(int nbp)
      {   int nbplace=nbp;
          occupe=new boolean[nbplace];
          for(int i=0;i<occupe.length;i++)  
                occupe[i]=false;
       }
       public int bookPlace(){
           for(i=0;i<nbplace;i++)
                if(!occupe[i]){  
                    
                    occupe[i]=true;//occupid
                   
                    break;
                 }
               
              return i;
        }
    }
    class Client extends Thread{
      static Parking v=new Parking(10);   public Client(String name){
                    super(name);
                    start();
         }
     
        public  void run(){
         System.out.println(getName()+"预定了第"+v.bookPlace()+"号车位!\n");
         }
        public static void main(String s[]){
                   new Client("dog");
                    new Client("cat");
                    new Client("mouse");
        }
    }
    按treeroot(旗鲁特) 的方法改了,但结果还是一样啊??????  
      
      

  7.   

    哪里改了?
    把 return 放到for外面还算勉强!
      

  8.   

    这样试试吧:
    class Parking
    {
        boolean occupe[];
        int nbplace;
        int i = 0;
        public Parking(int nbp)
        {
            nbplace = nbp;
            occupe = new boolean[nbplace];
            for (int i = 0; i < occupe.length; i++)
                occupe[i] = false;
        }    public synchronized int bookPlace()
        {
                for (; i < nbplace; i++)
                {
                    if (!occupe[i])
                    {
                        occupe[i] = true; //occupid
                        break;
                    }
                }
            return i;
        }
    }class Test extends Thread
    {
        private static Parking v = new Parking(10);    public Test(String name)
        {
            super(name);
            start();
        }    public void run()
        {
            System.out.println(getName() + "预定了第" + v.bookPlace() + "号车位!\n");
        }    public static void main(String s[])
        {
            new Test("dog");
            new Test("cat");
            new Test("mouse");
        }}
      

  9.   

    把Parking()方法中的
    int nbplace=nbp;
    改成
    nvplace=nbp;
    不然的话,类Parking中的变量nbplace的值为0,for循环没有进行,所以返回值是0
      

  10.   

    你运行的起啊 ~我怎么到JAVA 这步的时候 就报错 java.lang.suchnomthoderror:"main"
    那这是怎么了啊