CircularQueue测试错误,缩进

时间:2014-03-18 17:09:03

标签: java

以下是我的代码。我是java的新手。我测试中的一行出错了。这条线 x = Response.char At(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 TestCircularQueue2 {
    public static void main ( String [] args) {
        CircularQueue n = new CircularQueue ();
        Scanner in = new Scanner (System.in);
        String Response;
        char x;
        System.out.println("Enter command - i/d/l/p/q");
        Response = in.next ();
        x = Response.char At(0);
        x = character.toLowerCase (x);
        while (x != 'q' && x != 'Q') {
            switch (x) {
                    case 'i':
                    n.insertQueue ();
                    break;
                    case 'd':
                    n.deleteQueue();
                    break;
                    case 'l':
                    n.printQueueLogical();
                    break;
                    case 'y':
                    n.printQueuePhysical();
                    break;
                default:
                    System.out.println ("Illegal Response");
                    break;
            }
            System.out.println ("enter command - i/d/l/p/q");
            Response = in.next ();
            x = Response.char At(0);
            x = character.toLowerCase (x);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

该方法为charAt()而不是char At(),因此您应该更改行

x = Response.char At(0);

x = Response.charAt(0);

另请注意,该课程为Character而不是character,因此您应该使用:

Character.toLowerCase(x)

注意:尝试遵循Java命名约定。对变量/方法使用someVar等名称,对类使用SomeClass等名称。