Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                    
                
        

这是完整的代码,自从我从书中复制该程序以来,该程序应该可以运行:

Java:初学者指南:Herbert Schildt

类FixedQueue:

// A fixed-size queue class for characters that uses exceptions.
class FixedQueue implements ICharQ {
    private char q[]; // this array holds the queue
    private int putloc, getloc; // the put and get indices

    // Construct an empty queue given its size.
    public FixedQueue(int size) {
        q = new char[size]; // allocate memory for queue
        putloc = getloc = 0;
    }

    // Put a character into the queue.
    public void put(char ch) throws QueueFullException {

        if(putloc==q.length)
            throw new QueueFullException(q.length);

        q[putloc++] = ch;
    }

    // Get a character from the queue.
    public char get() throws QueueEmptyException {

        if(getloc == putloc)
            throw new QueueEmptyException();

        return q[getloc++];
    }
}


接口ICharQ:

// A character queue interface.
public interface ICharQ {
    // Put a character into the queue.
    void put (char ch) throws QueueFullException;

    // Get a character from the queue.
    char get() throws QueueEmptyException;
}


QExcDemo类:

// Demonstrate the queue exceptions.
class QExcDemo {
    public static void main(String args[]) {
        FixedQueue q = new FixedQueue(10);
        char ch;
        int i;

        try {
            // over-empty the queue
            for(i=0; i<11; i++) {
                System.out.print("Getting next char: ");
                ch = q.get();
                System.out.println(ch);
            }
        }
        catch(QueueEmptyException exc) {
            System.out.println(exc);
        }
    }
}


类QueueEmptyException:

// An exception for queue-empty errors.
public class QueueEmptyException extends Exception {

    public String toString() {
        return "\nQueue is empty.";
    }
}


类QueueFullException:

// An exception for queue-full errors.
public class QueueFullException extends Exception {
    int size;

    QueueFullException(int s) { size = s; }

    public String toString() {
        return "\nQueue is full. Maximum size is " + size;
    }
}


当我生成程序时,为什么会有关于异常的错误?


  运行:线程“主”中的异常java.lang.RuntimeException:
  无法编译的源代码-dec15_javatutorial.FixedQueue中的get()
  无法在dec15_javatutorial.ICharQ中实现get()
  方法不会抛出dec15_javatutorial.QueueEmptyException
  下一个字符:位于dec15_javatutorial.FixedQueue.get(FixedQueue.java:28)
    在dec15_javatutorial.QExcDemo.main(QExcDemo.java:33)处Java结果:1


谢谢

最佳答案

我已经将您所有的类都复制到一个项目中并运行它。它编译并运行。

输出:

Getting next char:
Queue is empty.


我的猜测是您已经更改了FixedQueue中的某些内容而忘记了保存,或者您导入了错误的内容。

关于java - java.lang.RuntimeException:无法编译的源代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27520331/

10-11 04:11