为什么我的方法读取(Scanner sc)出错?

时间:2011-11-24 19:53:58

标签: java java.util.scanner

我正在研究Read方法。我认为它会起作用,但它会在exp = new BinEx (ex1, calls, EX2)exp = new SimEx(token)上出错。

错误讯息为non-static variable can not be referenced from a static context

有谁知道问题是什么? Thanx回应史蒂文


import java.util.*;

public abstract class Ex {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Ex exp = read(sc);
        System.out.println(exp.toString());
        System.out.println(exp.eval());

    }

    public abstract String eval();

    /**
     * post: heeft een geldige Expression-String ingevoerd bestaande uit haakjes
     * operanden en operatoren, en heeft een Expression-object geretourneerd dat
     * is samengesteld uit uit SimpleExpressions en BinaryExpressions
     */
    public static Ex read(Scanner sc) {
        String token = sc.next();
        char res = token.charAt(0);
        Ex exp;
        if (res == '(') {
            Ex ex1 = read(sc);
            String operator = sc.next();
            char opr = operator.charAt(0);
            Ex ex2 = read(sc);
            String haakje = sc.next();
            exp = new BinEx(ex1, opr, ex2);
        } else
            exp = new SimEx(token);
        return exp;
    }

    class SimEx extends Ex {

        private String tekst;

        // post: heeft een SimpleExpression object gecreerd met tekst = tk
        public void SimEx(String tk) {
            tekst = tk;
        }

        // post: retouneert tekst
        public String eval() {
            return tekst;
        }

        // post: retourneert een String representatie van een SimpleExpression
        public String toString() {
            String res = "<SimEx(";
            return res + eval() + ")>";
        }
    }

    class BinEx extends Ex {

        private Ex   operand1;
        private char operator;
        private Ex   operand2;

        // post: heet een BinaryExpression object gecreëerd met operand1 = op1,
        // operator is or en operand2 = op2
        void BinEx(Ex op1, char or, Ex op2) {
            operand1 = op1;
            operator = or;
            operand2 = op2;
        }

        /**
         * post: indien (operator == "C") retourneert operand1 + operand2,
         * retourneert anders operand2 + operand1
         */
        public String eval() {
            BinEx res;
            if (operator == 'C')
                return operand1 + "" + operand2;
            else
                return operand2 + "" + operand1;
        }

        // post: retourneert een String representatie van een BinaryExpression
        public String toString() {
            String res = "<BinEx(";
            return (operand1.toString() + "," + operator + "," + operand2
                    .toString());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

由于我不会说你的语言,但我会讲Java,只需使用这个课程:

import java.util.*;

public  class Ex
{

public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    Ex exp = read(sc);
    System.out.println(exp.toString());
    System.out.println(exp.eval());
}

public  String eval(){return "";};

/**
 * post: heeft een geldige Expression-String ingevoerd bestaande uit haakjes
 * operanden en operatoren, en heeft een Expression-object geretourneerd dat
 * is samengesteld uit uit SimpleExpressions en BinaryExpressions
 */
public static Ex read(Scanner sc)
{
    String token = sc.next();
    char res = token.charAt(0);
    Ex exp;
    if (res == '(')
    {
        Ex ex1 = read(sc);
        String operator = sc.next();
        char opr = operator.charAt(0);
        Ex ex2 = read(sc);
        String haakje = sc.next();
        exp = new BinEx(ex1, opr, ex2);
    }
    else
        exp = new SimEx(token);
    return exp;
}
}
class SimEx extends Ex
{

    private String tekst;

    // post: heeft een SimpleExpression object gecreerd met tekst = tk
    public SimEx(String tk)
    {
        tekst = tk;
    }

    // post: retouneert tekst
    public String eval()
    {
        return tekst;
    }

    // post: retourneert een String representatie van een SimpleExpression
    public String toString()
    {
        String res = "<SimEx(";
        return res + eval() + ")>";
    }
}

class BinEx extends Ex
{

    private Ex operand1;
    private char operator;
    private Ex operand2;

    // post: heet een BinaryExpression object gecreëerd met operand1 = op1,
    // operator is or en operand2 = op2
    public BinEx(Ex op1, char or, Ex op2)
    {
        operand1 = op1;
        operator = or;
        operand2 = op2;
    }

    /**
     * post: indien (operator == "C") retourneert operand1 + operand2,
     * retourneert anders operand2 + operand1
     */
    public String eval()
    {
        BinEx res;
        if (operator == 'C')
            return operand1 + "" + operand2;
        else
            return operand2 + "" + operand1;
    }

    // post: retourneert een String representatie van een BinaryExpression
    public String toString()
    {
        String res = "<BinEx(";
        return (operand1.toString() + "," + operator + "," + operand2
                .toString());
    }
}

基本上你的括号搞砸了。

相关问题