有时输出数字不正确。

这是我的任务,给定一个表达式的输入,该表达式包含一串字母和运算符(加号,减号和字母.IE:’b-d + e-f’)和一个带有一组变量的文件/用逗号分隔的值对(即:a = 1,b = 7,c = 3,d = 14)写一个程序,输出输入表达式的结果。 例如,如果表达式输入为(“a + b + c -d”)并且文件输入为(a = 1,b = 7,c = 3,d = 14),则输出将为-3。 嗨,我正在尝试做一个简单的java代码,如果我添加4个字母输出一个数字。 当我做不同的组合,比如d-c + a + b时,它会给我一个像118.0这样的内含号。 有人能告诉我在我的代码中我的计算是错误的..

谢谢

ValVarPairs.txt包含这些数字 – > a = 100,b = 5,c = 10,d = 13

这就是我编码的内容。

package com.ecsgrid; import java.io.*; public class testC { public static void main(String[] args) { int i = 0,j = 0; double result, values[] = new double[4]; char k, operators[] = new char[3]; for (i = 0; i <= 2; i++) operators[i] = '+'; // default is to add the values File myfile; StreamTokenizer tok; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String InputText; i = 0; try { myfile = new File("C:\\VarValPairs.txt"); tok = new StreamTokenizer(new FileReader(myfile)); tok.eolIsSignificant(false); while ((tok.nextToken() != StreamTokenizer.TT_EOF) && (i <= 3)){ if ((tok.ttype == StreamTokenizer.TT_NUMBER)) values[i++] = tok.nval; } } catch(FileNotFoundException e) { System.err.println(e); return; } catch(IOException f) { System.out.println(f); return; } System.out.println("Enter letters and operators:"); try { InputText = in.readLine(); } catch(IOException f) { System.out.println(f); return; } for (i = 0; i < InputText.length(); i++) { k = InputText.charAt(i); if ((k == '+') || (k == '-')) { if (j <= 2) operators[j++] = k; } } result = values[0]; for (i = 0; i <= 2; i++){ if (operators[i] == '+') result = result + values[i+1]; else result = result - values[i+1]; } System.out.println(result); } } 

我不确定你的计算错在哪里,但你可以这样做:

编辑代码:

  import java.io.*; import java.util.*; public class test{ public static int a; public static int b; public static int c; public static int d; public static int fin = 0; public static String temp; public static void main(String[] args){ try{ Scanner input = new Scanner(new File("yourfile.txt")); temp = ""; while(input.hasNext()){ //stores the letters temp = temp + input.next(); } input.close(); } catch(Exception e){ } /* THIS IS IF THE FILE yourfile.txt IS IN THIS FORMAT EXACTLY: a=1 b=2 c=3 d=4 */ for(int i = 0; i < temp.length(); i++){ //intitializes the values String message = "" + temp.charAt(i); if(message.equals("a") || message.equals("b") || message.equals("c") || message.equals("d")){ String val = "" + temp.charAt(i+2); setValue(message,val); } } Scanner enter = new Scanner(System.in); System.out.print("ENTER EXPRESSION: "); String ex = enter.nextLine(); for(int b = 0; b < ex.length(); b++){ String m = ""+ ex.charAt(b); if(b == 0){ if(m.equals("a") || m.equals("b") || m.equals("c") || m.equals("d")){ fin = fin + getValue(m); } } else{ if(m.equals("a") || m.equals("b") || m.equals("c") || m.equals("d")){ String check = "" + ex.charAt(b-1); if(check.equals("+")){ fin = fin + getValue(m); } if(check.equals("-")){ fin = fin - getValue(m); } } } } System.out.println(fin); } public static void setValue(String variable, String value){ if(variable.equals("a")){ a = Integer.parseInt(value); } if(variable.equals("b")){ b = Integer.parseInt(value); } if(variable.equals("c")){ c = Integer.parseInt(value); } if(variable.equals("d")){ d = Integer.parseInt(value); } } public static int ret = 0; public static int getValue(String var){ if(var.equals("a")){ ret = a; } if(var.equals("b")){ ret = b; } if(var.equals("c")){ ret = c; } if(var.equals("d")){ ret = d; } return ret; } } 

您的代码中存在一些问题,您使用"=="而不是.equals()