CircularQueue程序正在出错

时间:2014-03-04 16:54:48

标签: java

我是java的新手。当我在终端上编译我的代码时,它告诉我我遇到了几个错误,我不知道为什么。大多数错误都是“找不到符号”

import java.util.Scanner;

public class CircularQueue {

    private int head, tail;
    private String [ ] q = new String [ 10 ];
    private String name;
    Scanner input = new Scanner (System.in);

    public CircularQueue () {
        head = -1;
        tail = -1;
    }

    public void insertQueue () {
        if (isQueueFull () )
            System.out.println ("Overflow");
        else {
            name = getName ();
            if (isQueueEmpty () )
                head = +1;
            if (tail==Size-1)
                tail=-1;
            q [++ tail] = name;
        }
    }
    public void deleteQueue() {
        String x;
        if ( isQueueEmpty () )
            System.out.println("Underflow");
        else {
            x=q[head];
            System.out.println ("Servicing " + x);
            if (head==tail) {
                head=-1;
                tail=-1;
            }
            else {
                head ++;
                if (head==Size)
                    head=0;
            }
        }
    }
    private String getName () {
        System.out.println("Enter name");
        return input.nextLine ();
    }
    public boolean isQueueEmpty () {
        return (head==-1);
    }
    public boolean isQueueFull () {
        return ((tail-head+1==0) || (tail-head+1==Size));
    }
    public void printQueueLogical () {
        int next;
        if (isQueueEmpty())
            System.out.println ("Empty");
        else {
            next=head;
            System.out.println (" q [" + next + "] = " +q[next]);
            while (next != tail) {
                next ++;
                if (next==Size)
                    next=0;
                System.out.println (" q [" + next + "] = " +q[next]);
            }
        }
    }
    public void printQueuePhysical () {
        for (int J=0; J<Size; J++)
            System.out.println (" q [" + J + "]= " + q [J]);
    }
    class TestCircularQueue {
        public static void main ( strings [] args) {
            CircularQueue n = new CircularQueue ();
            for (int J=0; J<3; J++)
            m.insertQueue ();
            m.deleteQueue ();
            m.printQueueLogical ();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

当编译器说它找不到符号时,这意味着您可能有语法错误。符号可以是关键字,某些方法名称,运算符等。

可能的原因,例如(从http://java.about.com/od/cerrmsg/g/Definition-Cannot-Find-Symbol.htm中提取)

  1. 尝试使用变量而不声明它。
  2. 拼写错误的类或方法名称(请记住,Java区分大小写)。
  3. 使用的参数与方法的签名不匹配。
  4. 使用导入声明未正确引用打包的类。
  5. 你可以做什么?

    您可以阅读堆栈跟踪。 “无法找到符号”例外通常带有有用的信息,例如违规的符号和代码中的位置。例如(再次,从上面的链接)

    System.out.prontln("The perils of mistyping..");
    

    将生成类似

    的内容
    cannot find symbol
    symbol: method prontln(jav.lang.String)
    location: class java.io.printStream
    

答案 1 :(得分:0)

刚刚添加了一些代码行。尽管测试你的逻辑,但不要创建不必要的内部类。

import java.util.Scanner;

public class CircularQueue {

private int head, tail;
private String [ ] q = new String [ 10 ];
private String name;
int Size;
Scanner input = new Scanner (System.in);

public CircularQueue () {
    head = -1;
    tail = -1;
}

public void insertQueue () {
    if (isQueueFull () )
        System.out.println ("Overflow");
    else {
        name = getName ();
        if (isQueueEmpty () )
            head = +1;
        if (tail==Size-1)
            tail=-1;
        q [++ tail] = name;
    }
}
public void deleteQueue() {
    String x;
    if ( isQueueEmpty () )
        System.out.println("Underflow");
    else {
        x=q[head];
        System.out.println ("Servicing " + x);
        if (head==tail) {
            head=-1;
            tail=-1;
        }
        else {
            head ++;
            if (head==Size)
                head=0;
        }
    }
}
public void setSize(int i)
{
    Size=i;
}
private String getName () {
    System.out.println("Enter name");
    return input.nextLine ();
}
public boolean isQueueEmpty () {
    return (head==-1);
}
public boolean isQueueFull () {
    return ((tail-head+1==0) || (tail-head+1==Size));
}
public void printQueueLogical () {
    int next;
    if (isQueueEmpty())
        System.out.println ("Empty");
    else {
        next=head;
        System.out.println (" q [" + next + "] = " +q[next]);
        while (next != tail) {
            next ++;
            if (next==Size)
                next=0;
            System.out.println (" q [" + next + "] = " +q[next]);
        }
    }
}
public void printQueuePhysical () {
    for (int J=0; J<Size; J++)
        System.out.println (" q [" + J + "]= " + q [J]);
}

}
class TestCircularQueue {
    public static void main ( String [] args) {
        CircularQueue n = new CircularQueue ();
        n.setSize(10);
        for (int J=0; J<3; J++)
        {n.insertQueue ();
        n.deleteQueue ();
        n.printQueueLogical ();}
    }
}

答案 2 :(得分:0)

我认为“大小”是指数组的长度.... 即。 int size = q.length;

并且出于某种原因,在创建类的对象时使用'n'然后当你想使用它时你用'm'检查也

所以在经过它之后......我认为dis对我有用......尽管你尝试了什么......但你应该自己排序其他

    import java.util.Scanner;

    public class CircularQueue {

        private int head, tail;
        private String [ ] q = new String [ 10 ];
        private String name;
        int Size = q.length;
        Scanner input = new Scanner (System.in);

        public CircularQueue () {
            head = -1;
            tail = -1;
        }

        public void insertQueue () {
            if (isQueueFull () )
                System.out.println ("Overflow");
            else {
                name = getName ();
                if (isQueueEmpty () )
                    head = +1;
                if (tail==Size-1)
                    tail=-1;
                q [++ tail] = name;
            }
        }
        public void deleteQueue() {
            String x;
            if ( isQueueEmpty () )
                System.out.println("Underflow");
            else {
                x=q[head];
                System.out.println ("Servicing " + x);
                if (head==tail) {
                    head=-1;
                    tail=-1;
                }
                else {
                    head ++;
                    if (head==Size)
                        head=0;
                }
            }
        }
        private String getName () {
            System.out.println("Enter name");
            return input.nextLine ();
        }
        public boolean isQueueEmpty () {
            return (head==-1);
        }
        public boolean isQueueFull () {
            return ((tail-head+1==0) || (tail-head+1==Size));
        }
        public void printQueueLogical () {
            int next;
            if (isQueueEmpty())
                System.out.println ("Empty");
            else {
                next=head;
                System.out.println (" q [" + next + "] = " +q[next]);
                while (next != tail) {
                    next ++;
                    if (next==Size)
                        next=0;
                    System.out.println (" q [" + next + "] = " +q[next]);
                }
            }
        }
        public void printQueuePhysical () {
            for (int J=0; J<Size; J++)
                System.out.println (" q [" + J + "]= " + q [J]);
        }
    }

public class TestCircular {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CircularQueue n = new CircularQueue ();
            n.insertQueue ();
            n.deleteQueue ();
            n.printQueueLogical ();
    }

}