-1
import java.util.*;

class BallList{
    private LinkedList<Integer> list = new LinkedList<Integer>();

    public BallList(int n){
        for (int i = 0; i < n; i++){
            list.add(i+1);
        }
    }

    public void DoAxy(int num1, int num2){
        Iterator iterator = list.iterator();
        int count = 0;
        while (iterator.hasNext()){
            count++;
            if (iterator.next().equals(num1)){
                break;
            }
        }
        int index = count;
        list.remove(num2);
        list.add(index,num2);
    }

    public void DoBxy(int num1, int num2){
        Iterator iterator = list.iterator();
        int count = 0;
        while (iterator.hasNext()){
            count++;
            if (iterator.next().equals(num1)){
                break;
            }
        }

        int index = count-1 ;
        list.remove(num2);
        list.add(index,num2);
    }

    public void DoRemove(int num){
        list.remove(num);
    }

    public LinkedList <Integer> getList(){
        return list;
    }
}

class Balls2{
    public static void main(String[] args){
        int num_balls, num_ops;
        String op;

        Scanner sc = new Scanner(System.in);

        num_balls = sc.nextInt();
        num_ops = sc.nextInt();

        BallList Arrayballs = new BallList(num_balls);

        for (int i = 0; i < num_ops; i++){
            op = sc.next();
            if (op.equals("A")){
                int ball_1 = sc.nextInt();
                int ball_2 = sc.nextInt();
                Arrayballs.DoAxy(ball_1, ball_2);
            }
            else if (op.equals("B")){
                int ball_1 = sc.nextInt();
                int ball_2 = sc.nextInt();
                Arrayballs.DoBxy(ball_1, ball_2);
            }
            else{
                int ball = sc.nextInt();
                Arrayballs.DoRemove(ball);
            }
        }
    }
    LinkedList<Integer> listballs = Arrayballs.getList();


}

当我运行上面的代码时,我得到了错误:

Balls2.java:80: error: cannot find symbol
    LinkedList<Integer> listballs = Arrayballs.getList();       
                                    ^
  symbol:   variable Arrayballs
  location: class Balls2
1 error

谁可以帮我这个事?

4

3 回答 3

2

这里的缩进没有帮助,但是您正在执行该行

LinkedList<Integer> listballs = Arrayballs.getList();  

main方法之外;由于 Arrayballs 是该方法的局部变量,因此无法从外部访问它。

于 2012-10-10T08:30:37.687 回答
1

Arrayballs您已经在您的方法内部进行了实例化,main并且您正试图从外部访问它。

于 2012-10-10T08:38:33.413 回答
1

您正在调用Arrayballs.getList()main 方法。它应该在里面 请检查大括号

于 2012-10-10T08:33:53.973 回答