java:159:错误:找不到合适的put方法(String,int)

时间:2018-04-30 01:15:13

标签: java

为什么说(String,int)?当我宣布它是双倍的时候?

java:159: error: no suitable method found for put(String,int)
                            myVariables.put(exprArray[i], 0);
                                       ^
method Map.put(String,Double) is not applicable
  (argument mismatch; int cannot be converted to Double)
method AbstractMap.put(String,Double) is not applicable
  (argument mismatch; int cannot be converted to Double)
method HashMap.put(String,Double) is not applicable
  (argument mismatch; int cannot be converted to Double)

1错误

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;


public class PostFixCalc {


public static boolean isCommandPromptOn = true;

public static HashMap<String,Double> myVariables = new HashMap<String, Double>();

public static String[] input;

public static boolean variableCalcuation = false;

public static void main(String[] args)
{
    Scanner myScanner = new Scanner(System.in);

    System.out.println("Integer postfix calculator with memory by XXXXXX");

    do
    {
        System.out.print("\n> ");
        input = myScanner.nextLine().split("\\s");
        commandOperator(input);

    }while(isCommandPromptOn);
}



public static void commandOperator(String[] command)
{
    variableCalcuation = false;

    if(command.length > 1)
    {

        if(command[1].equals("="))
        {
            variableCalcuation = true;
            double temp = postFixExpr(Arrays.copyOfRange(command,2,command.length));
            if(temp != -123) {
                myVariables.put(command[0], temp);
            }
            return;
        }
    }

    for (int i = 0; i < command.length; i++)
    {
        switch (command[i])
        {

            case "quit":
                isCommandPromptOn = false;
                break;
            case "clear":
                myVariables.clear();
                break;
            case "delete":
                myVariables.remove(command[i + 1]);
                break;
            case "var":
                System.out.println(myVariables);
                break;
            case "":
                System.out.println("Invalid input!");
                break;
            case "about":
                System.out.println("Postfix calculator by Samuel Fipps");
                System.out.println("Operators and functions: + - * / ^ sin cos tan sqrt");
                System.out.println("Commands: var clear quit about");
                break;
            default:
                postFixExpr(command);
                return;
        }
    }

}


public static double postFixExpr(String[] exprArray)
{
    double result = -123;
    Stack<Double> exprStack = new Stack <Double>();

    for (int i =0; i < exprArray.length;i++)
    {
        try {
            switch (exprArray[i]) {

                case "+":
                    double sum = exprStack.pop() + exprStack.pop();
                    exprStack.push(sum);
                    break;

                case "-":
                    double b = exprStack.pop();
                    double a = exprStack.pop();
                    exprStack.push(a - b);
                    break;

                case "*":
                    exprStack.push(exprStack.pop() * exprStack.pop());
                    break;

                case "/":
                    double b1 = exprStack.pop();
                    double a1 = exprStack.pop();
                    if (b1 == 0) {
                        System.out.println("Division by zero. Undefined.");
                        result = -123;
                        break;
                    }
                    exprStack.push((double) (a1 / b1));
                    break;

                case "^":
                    double b2 = exprStack.pop();
                    double a2 = exprStack.pop();
                    exprStack.push((double) (Math.pow(a2, b2)));
                    break;

               // case "!":
                   // double a3 = exprStack.pop();
                    //exprStack.push(factorialCalculator(a3));
                    //break;

                case "":
                    System.out.println("Invalid Input. Please try again!");
                    break;

                default:

                    if (myVariables.containsKey(exprArray[i])) {

                        exprStack.push(myVariables.get(exprArray[i]));

                    } else {

                        try {

                            exprStack.push(Double.parseDouble(exprArray[i]));

                        } catch (Exception e) {

                            myVariables.put(exprArray[i], 0);

                            try {

                                exprStack.push(myVariables.get(exprArray[i]));

                            } catch (Exception e1) {
                                System.out.println("Error: " + e);
                            }

                        }
                    }
                    break;
            }
        }
        catch (Exception e){
            if(!variableCalcuation){

                System.out.println("Error! Too many operators.");

            }else
            {
                System.out.println("Error! Too many operators. ");
                return -123;
            }
        }

    }

    if(exprStack.size() == 0){
        if(variableCalcuation){
            return -123;
        }
        return result;
    }
    else if(exprStack.size() == 1 ){
        result = exprStack.pop();
        System.out.println(result);
        return result;
    }
    else
    {
        if(variableCalcuation){
            System.out.println("Error! Not enough operators.");
            return -123;
        }

        System.out.println("Error! Not enough operators.");
        return result;
    }

}

public static double factorialCalculator(double num){

    if(num == 0){
        return 1;
    }
    else{
        return num * factorialCalculator(num -1);
    }

}


  }

1 个答案:

答案 0 :(得分:3)

int无法隐式转换为Double,但doubleDouble即可。

更改

myVariables.put(exprArray[i], 0);

myVariables.put(exprArray[i], 0.0);

将解决您的问题。