Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编译堆栈声明时出错_Java_Variables_Compiler Errors_Stack_Calculator - Fatal编程技术网

Java 编译堆栈声明时出错

Java 编译堆栈声明时出错,java,variables,compiler-errors,stack,calculator,Java,Variables,Compiler Errors,Stack,Calculator,所以我对java还不熟悉,我正在尝试制作一个带有一些典型错误控制的计算器,但我似乎无法让它工作,我有点卡住了。我真的很感激你能帮我找出故障的原因。编译器给了我这个 Exception in thread "main" java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at ergasia.ioanna.ErgasiaIoanna.check(ErgasiaIoanna.java:38) at ergasia

所以我对java还不熟悉,我正在尝试制作一个带有一些典型错误控制的计算器,但我似乎无法让它工作,我有点卡住了。我真的很感激你能帮我找出故障的原因。编译器给了我这个

Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at ergasia.ioanna.ErgasiaIoanna.check(ErgasiaIoanna.java:38)
at ergasia.ioanna.ErgasiaIoanna.main(ErgasiaIoanna.java:101)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)


异常
java.util.EmptyStackException
表示您正在尝试对空堆栈执行操作,在本例中为
peek
。在
main
方法中,声明两个
Stack
s
Tel
Ar
。都是空的。然后将这些空堆栈传递给
check
方法,该方法尝试
peek
查看它们的第一个元素,这是在获得
EmptyStackException

时,这不是编译错误。这是程序的运行时错误。添加一些调试打印或通过调试器运行程序以找出错误。
package ergasia.ioanna;

import java.util.*;
import java.io.*;

public class ErgasiaIoanna {

    static void praksi(Stack telestes,Stack arithmoi){
    double res;
    Character temp = (Character) telestes.pop();
    Double ar2 = (Double) arithmoi.pop();
    Double ar1 = (Double) arithmoi.pop();
    if(temp=='+'){
        res=ar1+ar2;
    }
     if(temp=='-'){
        res=ar1-ar2;
    }
     if(temp=='*'){
        res=ar1*ar2;
    }
     if(temp=='/'){
        if(ar2==0){
            System.out.println("error");
        }
        else{
            res=ar1/ar2;
        }
    }
     if(temp=='^'){
        res=Math.pow(ar1,ar2);
    }
        arithmoi.push( new Double (res) );

    }
    static void check(char i,Stack telestes,Stack arithmoi){
        int error=0;
        char cha = (Character) telestes.peek();
        double ar = (Double) arithmoi.peek();
        if(i==')'){
           if(telestes.empty() || cha=='('){
               System.out.println("error");
               error=1;
           }
           do{
               praksi(telestes,arithmoi);
               cha=(char) telestes.peek();
               if(telestes.empty()){
                 System.out.println("error");
                 cha='(';
               }
           }while(cha!='(');
        }
        if(i=='='){
            while(!telestes.empty()){
                praksi(telestes,arithmoi);
                cha = (Character) telestes.peek();
            }
           System.out.print(arithmoi.pop());
        }
        if(i=='+' || i=='-'){
            if(!telestes.empty() || cha!='('){
                praksi(telestes,arithmoi);
                telestes.push(i);
            }
            else{
               telestes.push(i);
            }
        }
        if(i=='*' || i=='/'){
            if(cha=='^'){
             praksi(telestes,arithmoi);
               telestes.push(i);  
            }
            else{
                telestes.push(i);
            }
        }
        if(i=='^'){
            if(!telestes.empty() || cha!='('){
                praksi(telestes,arithmoi);
                telestes.push(i);
        }
        else{
            telestes.push(i);
        }

    }
    }



    public static void main(String[] args)throws IOException {
        Stack Tel = new Stack();
        Stack Ar = new Stack();
        char c;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        do {
         c = (char) br.read();
         check(c,Tel,Ar);
      } while(c != 's');
    }
}