0
好吧我對java很陌生。我正在創建一個Postfix計算器,我正在嘗試使用hashmap爲它創建一個內存。用戶應該能夠分配他/她自己的變量,例如:將問題存儲在hashmap中存在問題
> a = 3 5 + 1 -
7
> bee = a 3 *
21
> a bee +
28
> bee 3 %
0
> a = 4
4
> 57
57
> 2 c +
c not found
> mem
a: 4
bee: 21
> exit
用戶使用「VAR =」來分配變量,以後可以用它來調出之前的答案。 我不能讓我的計算方法忽略了「A =」,所以它返回一個錯誤,並且當我運行該程序,並嘗試使用一個變量,我得到了錯誤
>Exception in thread "main" java.lang.NumberFormatException: For input string: "
Error"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Program6.main(Program6.java:38)
有什麼好辦法以某種方式讓我的計算方法忽略變量輸入,以及實現散列表的最佳方式是什麼?我似乎陷入了困境。這裏是到目前爲止
import java.util.*;
import java.io.*;
public class Program6
{
private static HashMap<String,Integer> memory = new HashMap<>();
public static void main(String args[])
{
System.out.println("Servando Hernandez");
System.out.println("RPN command line calculator");
Scanner scan = new Scanner(System.in);
System.out.print(">");
while(scan.hasNextLine())
{
System.out.print("> ");
String a = scan.nextLine();
String b = "quit";
String c = "mem";
String d = "clear";
if(a.equals(b))
{
System.exit(0);
}
else
{
System.out.println(compute(a));
}
System.out.print(">");
List<String> list = new ArrayList<String>();
if(!a.isEmpty())
{
StringTokenizer var = new StringTokenizer(a);
while(var.hasMoreTokens())
{
list.add(var.nextToken());
}
}
int pos = Integer.parseInt(compute(a));
memory.put(list.get(l.size()-1),pos);
}
}
public static String compute(String input)
{
List<String> processedList = new ArrayList<String>();
if (!input.isEmpty())
{
StringTokenizer st = new StringTokenizer(input);
while (st.hasMoreTokens())
{
processedList.add(st.nextToken());
}
}
else
{
return "Error";
}
Stack<String> tempList = new Stack<String>();
Iterator<String> iter = processedList.iterator();
while (iter.hasNext())
{
String temp = iter.next();
if (temp.matches("[0-9]*"))
{
tempList.push(temp);
}
else if (temp.matches("[*-/+]"))
{
if (temp.equals("*"))
{
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls * rs;
tempList.push("" + result);
}
else if (temp.equals("-"))
{
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls - rs;
tempList.push("" + result);
}
else if (temp.equals("/"))
{
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls/rs;
tempList.push("" + result);
}
else if (temp.equals("+"))
{
int rs = Integer.parseInt(tempList.pop());
int ls = Integer.parseInt(tempList.pop());
int result = ls + rs;
tempList.push("" + result);
}
}
else
{
return "Error";
}
}
return tempList.pop();
}
}
對不起,我猜想大家都知道PostFix計算器是什麼。用戶必須輸入每個參數的空格,並且操作符會在輸入整數之後執行。 – Servanh 2015-04-03 04:07:56
我輸入'1 + 1',並在計算方法代碼'else if(temp.equals(「+」)) {//你先彈出,你的tempList將爲空,所以當你再次彈出時,異常 int rs = Integer.parseInt(tempList.pop()); int ls = Integer.parseInt(tempList.pop()); int result = ls + rs; tempList.push(「」+ result); }' – phony 2015-04-03 06:20:25
如何在註釋中創建新行,太難顯示代碼 – phony 2015-04-03 06:25:44