繁体   English   中英

CircularQueue程序出错

[英]CircularQueue program is getting errors

我是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 ();
        }
    }
}

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

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

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

你可以做什么?

您可以阅读stacktrace。 “找不到符号”异常通常带有有用的信息,例如有问题的符号和代码中的位置。 例如(同样,通过上面的链接)

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

会产生类似

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

只需添加一些代码行即可.Dint会测试您的逻辑,并且不会创建不必要的内部类。

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 ();}
    }
}

我认为“大小”是指数组的长度。 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 ();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM