我正在用java编写一个计算器,它有内存来存储变量。它正确显示变量列表,加减,并定义变量,但每当我尝试执行 a = b + 1 之类的操作时,它会将 b 视为 0,将“a”的值设为 1。
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
ArrayList<Integer> values = new ArrayList<>();
ArrayList<String> variables = new ArrayList<>();
while(scan.hasNextLine())
{
String line = scan.nextLine();
String[] tokens = line.split(" ");
if(!Character.isDigit(tokens[0].charAt(0)) && !line.equals("clear") && !line.equals("var"))
{
int value = 0;
for(int i=0; i<tokens.length; i++)
{
if(tokens.length==1)
{
if(variables.contains(tokens[0]))
{
printAnswer(values.get(variables.indexOf(tokens[0])));
}
else if(!line.equals("quit"))
{
int width = 4 + tokens[0].length();
System.out.printf("%" + width + "s is not defined\n", tokens[0]);
}
break;
}
if(tokens.length==3)
{
value = Integer.parseInt(tokens[2]);
printAnswer(value);
if(variables.contains(tokens[0]))
{
values.set(variables.indexOf(tokens[0]), value);
variables.set(variables.indexOf(tokens[0]), tokens[0]);
}
else
{
values.add(value);
variables.add(tokens[0]);
}
break;
}
else if(tokens[i].charAt(0) == '+')
{
value = addition(tokens, value, variables, values);
printAnswer(value);
if(variables.contains(tokens[0]))
{
values.set(variables.indexOf(tokens[0]), value);
}
else
{
values.add(value);
variables.add(tokens[0]);
}
break;
}
else if(tokens[i].charAt(0) == '-')
{
value = subtraction(tokens, value);
printAnswer(value);
if(variables.contains(tokens[0]))
{
values.set(variables.indexOf(tokens[0]), value);
}
else
{
values.add(value);
variables.add(tokens[0]);
}
break;
}
else if(i==tokens.length-1)
{
System.out.println("No operation");
break;
}
}
}
if(Character.isDigit(tokens[0].charAt(0)))
{
int value = 0;
for(int i=0; i<tokens.length; i++)
{
if(tokens.length==1)
{
value = Integer.parseInt(tokens[0]);
printAnswer(value);
break;
}
else if(tokens[i].charAt(0) == '+')
{
value = addition(tokens, value, variables, values);
printAnswer(value);
}
else if(tokens[i].charAt(0) == '-')
{
value = subtraction(tokens, value);
printAnswer(value);
}
}
}
if(line.equals("clear"))
{
clear(variables, values);
}
if(line.equals("var"))
{
variableList(variables, values);
}
else if(line.equals("quit"))
{
System.exit(0);
}
}
}
public static int addition(String[] a, int b, ArrayList<String> c, ArrayList<Integer> d)
{
for(String item : a)
{
int i=0;
if(Character.isDigit(item.charAt(0)))
{
int val = Integer.parseInt(item);
b = b + val;
i++;
}
else if(Character.isLetter(item.charAt(0)) && i!=0)
{
int val = d.get(c.indexOf(item));
b = b + val;
}
}
return b;
}
public static int subtraction(String[] a, int b)
{
int i=0;
for(String item : a)
{
if(Character.isDigit(item.charAt(0)))
{
int val = Integer.parseInt(item);
if(i==0)
{
b = b + val;
i++;
}
else
{
b = b - val;
}
}
}
return b;
}
public static void clear(ArrayList<String> a, ArrayList<Integer> b)
{
a.clear();
b.clear();
}
public static void variableList(ArrayList<String> a, ArrayList<Integer> b)
{
for(int i=0; i<a.size(); i++)
{
int width = 4 + a.get(i).length();
System.out.printf("%" + width + "s: %d\n", a.get(i), b.get(i));
}
}
public static void printAnswer(int a)
{
int width = 3;
Integer valueToString = a;
String valueString = valueToString.toString();
for(int j=0; j<=valueString.length(); j++)
{
width++;
}
System.out.printf("%" + width + "d\n",a);
}
主要问题在加法中