简体   繁体   English

方法中的私有类-Java

[英]private classes in method - java

I'm trying to write a program that takes an input of algebraic expressions (ie (6*3)+(7-2)) in what is apparently polish form (ie 6,3,'*',7,2,'-','+'), by putting integers on the stack and calling operands on the top two elements, then finally returning whatever's on top of the stack (hopefully there's only one element - the answer - on the stack). 我正在尝试编写一个程序,该程序以明显的波兰形式(即6,3,'*',7,2,')接受代数表达式(即(6 * 3)+(7-2))的输入-','+'),方法是将整数放在堆栈上,并在前两个元素上调用操作数,然后最后返回堆栈上的所有内容(希望堆栈上只有一个元素-答案)。

I'm very new to java, so I'm sure that this is a thoroughly mundane question, but I can't seem to make my stack I'm using (named 'store') accessable and a editable by other methods in the class. 我是Java的新手,所以我确定这是一个非常平凡的问题,但是我似乎无法使正在使用的堆栈(名为“ store”)可以被其他方法访问和编辑。类。

My implementation of stack seems to work fine, I'll post it if it comes 'under fire'. 我的堆栈实现似乎工作正常,如果发生“火灾”,我将其发布。

My problem arises when the function has to call one of the operand subfunctions, like add(). 当函数必须调用操作数子函数之一(如add())时,就会出现我的问题。 It seems to totally forget that store exists and has been edited, and instead treats it as a null stack. 似乎完全忘记了存储存在并且已经被编辑,而是将其视为空堆栈。 I tried throwing 'this' in front of everything except the first two instances, but that didn't do too much. 我尝试将“ this”扔到除了前两个实例之外的所有事物的前面,但这并不过分。 How can I get the function to share 'store' throughout all of its functions? 如何获得该功能以在其所有功能之间共享“存储”?

Sample run: 样品运行:

Eval myEval = new Eval;
myEval.evaluate(6, 3, '+', 4, 9, '*', '-'); 

Exception in thread "main" java.lang.NullPointerException
at Eval.add(Eval.java:45)
at Eval.evaluate(Eval.java:13)

public class Eval {
     public stack store;
     public Eval(){
        stack store = new stack();
    }

    public int evaluate(Object ... parts){
        for(Object part: parts){
            if(part instanceof Character){
                char operator = (Character) part;
                if(operator=='+'){
                    add();
                }
                else if(operator=='-'){
                    subtract();
                }
                else if(operator=='*'){
                    multiply();
                }
                else if(operator=='/'){
                    divide();
                }
                else{
                    throw new IllegalArgumentException("Illegal operand");
                }
            }
            else if(part instanceof Integer){
                int number = (Integer)part;
                store.push(part);
                System.out.println(store.peek());
            }
            else{
                throw new IllegalArgumentException("Expected char or int");
            }
        }
        if(store.peek()==null){
            throw new IndexOutOfBoundsException("No value to return");
        }
        return (int)store.pop();
    }
    private void add(){ 
        int x=(int)store.pop(); //error occurs on this line
        x+=(int)store.pop(); 
        store.push(x);
    }
    private void subtract(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        x=y-x;
        store.push(x);
    }
    private void multiply(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        x=y*x;
        store.push(x);
    }
    private void  divide(){
        int x=(int)store.pop();
        int y=(int)store.pop();
        int z = Math.floorDiv(x,y);
        store.push(z);
    }
}

It looks like your constructor, Eval(), assigns a new stack to a temporary variable, named "store", which isn't the Eval class's public "store" variable. 看起来您的构造函数Eval()将新堆栈分配给名为“ store”的临时变量,该临时变量不是Eval类的公共“ store”变量。 Consequently, the instance variable, "store", is null when Add is entered. 因此,输入Add时,实例变量“ store”为null。 Change the implementation of Eval() to: 将Eval()的实现更改为:

public Eval(){
    this.store = new stack();
}

Or else, create a getter method for store (getStore) and instantiate it, if null, in that method, though, if you did that, you'd have to remember to reference it via getStore(), instead of directly. 否则,请为store(getStore)创建一个getter方法,并在该方法中将其实例化(如果为null),但是如果这样做,则必须记住要通过getStore()而不是直接对其进行引用。

Very likely you are trying to pop when the stack is empty. 当堆栈为空时,您很有可能尝试弹出。 Also, it is unnecessary to cast the assignment for x when you already know the stack will contain an int. 同样,当您已经知道堆栈将包含一个int时,也不必对x进行赋值转换。 int x=(int)store.pop(); int x =(int)store.pop(); can just be int x = store.pop(); 可以只是int x = store.pop();

I would recommend adding a check to your operator functions such as: 我建议向您的操作员功能添加检查,例如:

if(stack.getLength() > 0) {
    //then add operation
} else {
  //throw an error
}

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

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