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 尝试将double转换为字符串时无法解析的变量_Java_Variables_Queue_Calculator - Fatal编程技术网

Java 尝试将double转换为字符串时无法解析的变量

Java 尝试将double转换为字符串时无法解析的变量,java,variables,queue,calculator,Java,Variables,Queue,Calculator,在此之前,我为调车场算法编写了一个方法,这里我想创建一个方法,以便计算后缀表达式。我将调用这个计算方法,以便在后缀队列上执行它 public String calculate(Queue post, Stack polish) { 我将退出队列,并将其分成单独的令牌进行读取 String token = post.Dequeue(); while(!(token==null)) { if(isOperator(token)) { dou

在此之前,我为调车场算法编写了一个方法,这里我想创建一个方法,以便计算后缀表达式。我将调用这个计算方法,以便在后缀队列上执行它

public String calculate(Queue post, Stack polish) {
我将退出队列,并将其分成单独的令牌进行读取

    String token = post.Dequeue();
    while(!(token==null)) {
        if(isOperator(token)) {
            double operand_2 = Double.parseDouble(polish.pop());
            double operand_1 = Double.parseDouble(polish.pop());
            if(token.contains("+")) {
                double result = operand_2 + operand_1;
            }
            else if(token.contains("-")) {
                double convert = operand_2 - operand_1;
            }
            else if(token.contains("/")) {
                double convert = operand_2/operand_1;
            }
            else if(token.contains("*")) {
                double convert = operand_2/operand_1;
            }
当有一个令牌要读取时

    String token = post.Dequeue();
    while(!(token==null)) {
        if(isOperator(token)) {
            double operand_2 = Double.parseDouble(polish.pop());
            double operand_1 = Double.parseDouble(polish.pop());
            if(token.contains("+")) {
                double result = operand_2 + operand_1;
            }
            else if(token.contains("-")) {
                double convert = operand_2 - operand_1;
            }
            else if(token.contains("/")) {
                double convert = operand_2/operand_1;
            }
            else if(token.contains("*")) {
                double convert = operand_2/operand_1;
            }
当我尝试将“convert”转换为字符串时,它告诉我convert不能作为变量解析

            String result = Double.toString(convert);
            polish.push(result);
        }
        else if(isNumeric(token)){
            polish.push(token);
        }
        String finalVal = polish.pop();
        return finalVal;
    }
}

这是一个范围问题。当您声明一个变量时,声明将一直持续到下一个
}
字符,而那些匹配的
{
字符已经通过的字符除外。因为您在每个
if
else if
块中声明
convert
,所以声明将一直持续到该块的末尾

您需要做的是在所有
if
else
语句之前声明
double convert;
,这样声明将一直持续到您需要使用
convert
的时候


您可能还想给它一个初始值,例如
double convert=0;
,以处理运算符与任何
if
else if
语句不匹配的情况。否则,您可能会出现不同的编译错误。

这是一个范围问题。当您声明变量时,声明将持续在下一个
}
字符之前,在那些匹配的
{
字符已经通过的字符中。因为您在每个
if
else if
块中声明
convert
,所以声明将持续到该块的末尾

您需要做的是在所有
if
else
语句之前声明
double convert;
,这样声明将一直持续到您需要使用
convert
的时候


您可能还想给它一个初始值,例如
double convert=0;
,以处理运算符与任何
if
else if
语句不匹配的情况。否则,您可能会出现不同的编译错误。

您没有在该范围内定义
convert
。这不是declar作为一个变量,你会得到这个错误

更改为:

    double convert;
    while(!(token==null)) {
            if(isOperator(token)) {
                double operand_2 = Double.parseDouble(polish.pop());
                double operand_1 = Double.parseDouble(polish.pop());
                if(token.contains("+")) {
                    double result = operand_2 + operand_1;
                }
                else if(token.contains("-")) {
                    convert = operand_2 - operand_1;
                }
                else if(token.contains("/")) {
                    convert = operand_2/operand_1;
                }
                else if(token.contains("*")) {
                    convert = operand_2/operand_1;
                }

您没有在该范围内定义
convert
。它没有声明为变量,因此会出现该错误

更改为:

    double convert;
    while(!(token==null)) {
            if(isOperator(token)) {
                double operand_2 = Double.parseDouble(polish.pop());
                double operand_1 = Double.parseDouble(polish.pop());
                if(token.contains("+")) {
                    double result = operand_2 + operand_1;
                }
                else if(token.contains("-")) {
                    convert = operand_2 - operand_1;
                }
                else if(token.contains("/")) {
                    convert = operand_2/operand_1;
                }
                else if(token.contains("*")) {
                    convert = operand_2/operand_1;
                }

请不要忘记投票和接受请不要忘记投票和接受