非静态变量,这不能从静态上下文错误中引用

时间:2014-02-08 10:33:27

标签: java

import java.util.Scanner;

public class Computer {
    String brand;
    String serialNo;
    int year;
    String type;
    double price;

    public Computer(String a, String b, int c, String d, double e) {
        brand = a;
        serialNo = b;
        year = c;
        type = d;
        price = e;

    }

    public String getbrand() {
        return brand;
    }

    public String getserialNo() {
        return serialNo;
    }

    public int getyear() {
        return year;
    }

    public String gettype() {
        return type;
    }

    public double getprice() {
        return price;
    }

    public static void main(String[] args) {
        LinkedList computer = new LinkedList();
        LinkedList notebooklist = new LinkedList();
        LinkedList desktoplist = new LinkedList();

        Scanner scan = new Scanner(System.in);

        for (int i = 0; i < computerList.size(); i++) {
            System.out.println("Please enter brand ");
            brand = scan.next();
            System.out.println("Please enter serial number ");
            serialNo = scan.next();
            System.out.println("Please enter year manufactured ");
            year = scan.nextInt();
            System.out.println("Please enter type ");
            type = scan.next();
            System.out.println("Please enter price ");
            price = scan.nextDouble();

            Computer data = new Computer(brand, serialNo, year, type, price);
            computerList.insertAtBack(data);
        }
    }

    public class LinkedList {
        private Node first;//the 1st node
        private Node last;//the last node
        private Node current; //if any

        public LinkedList() // default constructor
        {
            first = null;//set the linked list to null
            last = null;
            current = null;
        }

        public boolean isEmpty() {
            return (first == null);
        }

        public void insertAtBack(Object insertItem) {
            Node newNode = new Node(insertItem);

            if (isEmpty()) {
                first = newNode;
                last = newNode;
            } else {
                last.next = newNode;
                last = newNode;
            }
        }

        public Object getFirst() {
            if (isEmpty()) {
                return null;
            } else {
                current = first;
                return current.data;
            }
        }

        public Object getNext() {

            if (current == last) {
                return null;
            } else {
                current = current.next;
                return current.data;
            }

        }

        class Node {
            Object data;
            Node next;

            Node() {
                data = null;
            }

            Node(Object obj) {
                data = obj;
            }

            Node(Object obj, Node a) {
                data = obj;
                a = next;
            }
        }
    }
}

这是我的任务。我因为'非静态变量而无法从静态上下文引用'错误而被卡住了。我不知道是什么导致它。有人向我解释?这是使用链表的正确方法吗?

0 个答案:

没有答案