while(!q1.isFull())while(!q1.isEmpty())你的这两句已经决定了肯定不会抛出异常的啦
呵呵

解决方案 »

  1.   

    感觉楼住的程序
    boolean isEmpty()
    {
    if(c.length==0)
    return true ;
    else
    return false ;
    }
    用不到呀!!!不可能为空.
      

  2.   

    我把程序的功能和大家说一下,用数组实现一个char队列类,该类的构造函数传递一个int类型的参数,指出队列的大小,操作有put和get,队列为满时再put或队列为空时再get,要用异常处理(定义自己的异常类)。
      

  3.   

    public static void main(String[] args)
    {
    CharQueue q1 = new CharQueue(100) ;
    //CharQueue q2 = new CharQueue(4) ;

    try
    {
    for(char i='a'; i<='z'; i++)
    q1.put(i) ;

    }catch(FullQueueException e)
    {
    System.out.println(e.toString()) ;
    }

    try
    {
    for(char i='a'; i<='z'; i++)
    System.out.print(q1.get()) ;

    }catch(EmptyQueueException ee)
    {
    System.out.println(ee.toString()) ;
    }


    }
      

  4.   

    把while()判断语句去掉~~
    没有什么用。
    尽管在执行put方法时可能出现异常情况,但是后面有catch语句进行捕获。
    如果有while的话,那么put方法中就不可能出现异常,所以就不会执行catch语句;
      

  5.   

    if(c.length>=capacity)
    你数组一但定义了.他的c.length就是capacity.所以他无论什么情况都相等的.
      

  6.   

    楼上的有道理
    这样就OK了
    boolean isEmpty()
    {
    if(back==0)
    return true ;
    else
    return false ;
    }

    boolean isFull()
    {
    if(back==capacity+1)
    return true ;
    else
    return false ;
    }
      

  7.   

    import java.util.* ;
    import java.io.* ;
    class TestCharQueue
    {
    public static void main(String[] args)
    {
    CharQueue q1 = new CharQueue(100) ;
    //CharQueue q2 = new CharQueue(4) ;

    try
    { //while(!q1.isFull())
    //{
    for(char i='a'; i<='z'; i++)
    q1.put(i) ;
    //}
    }catch(FullQueueException e)
    {
    System.out.println(e.toString()) ;
    }

    try
    {
    for(char i='a'; i<=200; i++)
    System.out.print(q1.get()) ;

    }catch(EmptyQueueException ee)
    {
    System.out.println(ee.toString()) ;
    }


    }
    }
    class CharQueue
    { int top=0;
    int capacity  ;
    char[] c ;
    public CharQueue(int capacity)
    {
    this.capacity = capacity ;
    c = new char[capacity] ;
    }

    boolean isEmpty()
    {
    if(top==0)
    return true ;
    else
    return false ;
    }

    boolean isFull()
    {
    if(top>=capacity)
    return true ;
    else
    return false ;
    }

    public void put(char c1) throws FullQueueException//入队列
    {
    if(isFull())
    throw new FullQueueException() ;
    else
    c[top++] = c1 ;


    }

    public char get() throws EmptyQueueException//出队列
    { char c1 ;
    if(isEmpty())
    throw new EmptyQueueException() ;
    else
    c1 = c[top--] ;

    return c1 ;
    }
    }class EmptyQueueException extends Exception
    {
    CharQueue sourceQueue ;

    public EmptyQueueException()
    {

    }

    public EmptyQueueException(CharQueue q)
    {
    super("队列已空。") ;
    sourceQueue = q ;
    }

    public String toString()
    {
    return("队列" + "已经为空,执行出队操作引发异常") ;
    }
    }class FullQueueException extends Exception
    {
    CharQueue sourceQueue ;

    public FullQueueException()
    {

    }

    public FullQueueException(CharQueue q)
    {
    super("队列已满") ;
    sourceQueue = q ;
    }

    public String toString()
    {
    return("队列" + "队列已满,执行入队操作引发异常") ;
    }
    }
    你调式一下看是否符合你的要求???
      

  8.   

    vgvg,你是pku的吗?
    如果是的话,偶们一个系的了,呵呵
      

  9.   

    import java.util.* ;
    import java.io.* ;
    class TestCharQueue
    {
    public static void main(String[] args)
    {
    CharQueue q1 = new CharQueue(100) ;
    CharQueue q2 = new CharQueue(4) ;

    try
    {
    for(char i='a'; i<='z'; i++)
    q1.put(i) ;
    }
    catch(FullQueueException e1)
    {
    System.out.println(e1.toString()) ;
    }

    try
    {
    for(char i='a'; i<='z'; i++)
    System.out.print(q1.get()) ;
    }
    catch(EmptyQueueException e2)
    {
    System.out.println(e2.toString()) ;

    }
    System.out.println("\n") ;
    System.out.println("下面输出q2队列的字符") ;
    System.out.println("\n") ;
    try
    {
    q2.put('z') ;
    q2.put('y') ;
    q2.put('x') ;
    q2.put('w') ;
    q2.put('v') ;
    }
    catch(FullQueueException e3)
    {
    System.out.println(e3.toString()) ;
    }

    try
    {
    System.out.print(q2.get()) ;
    System.out.print(q2.get()) ;
    System.out.print(q2.get()) ;
    System.out.print(q2.get()) ;
    //System.out.print(q2.get()) ;
    }
    catch(EmptyQueueException e4)
    {
    System.out.println(e4.toString()) ;

    }



    }
    }
    class CharQueue
    {
    int front = 0 ;
    int back = 0 ;
    int capacity  ;
    char[] c ;
    public CharQueue(int capacity)
    {
    this.capacity = capacity ;
    c = new char[capacity] ;
    }

    boolean isEmpty()
    {
    if(back==0)
    return true ;
    else
    return false ;
    }

    boolean isFull()
    {
    if(back==capacity+1)
    return true ;
    else
    return false ;
    }

    public void put(char c1) throws FullQueueException//入队列
    {
    if(isFull())
    throw new FullQueueException(this) ;
    else
    c[back++] = c1 ;

    }

    public char get() throws EmptyQueueException//出队列
    { char c1 ;
    if(isEmpty())
    throw new EmptyQueueException(this) ;
    else
    c1 = c[front++] ;
    if(2*front>=capacity)
    {
    for(int i=0; i<back-front; i++)
    c[i] = c[i+front] ;
    back -=front ;
    front = 0 ;
    }
    return c1 ;
    }
    }class EmptyQueueException extends Exception
    {
    CharQueue sourceQueue ;

    public EmptyQueueException()
    {

    }

    public EmptyQueueException(CharQueue q)
    {
    super("队列已空。") ;
    sourceQueue = q ;
    }

    public String toString()
    {
    return("队列" + sourceQueue.toString() + "已经为空,执行出队操作引发异常") ;
    }
    }class FullQueueException extends Exception
    {
    CharQueue sourceQueue ;

    public FullQueueException()
    {

    }

    public FullQueueException(CharQueue q)
    {
    super("队列已满") ;
    sourceQueue = q ;
    }

    public String toString()
    {
    return("队列" + sourceQueue.toString() + "队列已满,执行入队操作引发异常") ;
    }
    }为什么捉不到我定义的异常信息啊?
      

  10.   

    问题解决了,呵呵boolean isFull()
    {
    if(back>=capacity)
    return true ;
    else
    return false ;
    }
      

  11.   

    另外100分我在下面这个帖子和你接,你们到那边只要up就好了http://community.csdn.net/Expert/topic/3636/3636721.xml?temp=.2500879