2013-10-24 36 views
-2

這是我的任務,給定一個由一串字母和運算符組成的表達式(加號,減號和字母,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這樣的inccorect數字。誰能告訴我,在我的代碼我的計算是錯誤的..有時輸出的數字不正確。

謝謝

的ValVarPairs.txt包含這些numbers-> 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); 
} 
} 
+0

這不是一個類的任務,即時嘗試自學java。非常感謝你。如果你可以幫忙,那麼請幫助你把自己的聰明話留給自己。 – soupi7

+0

http://stackoverflow.com/questions/19572320/output-incorrect/19572400#19572400在你提出的重複問題中回答了它。 '@ [A mod or someone who knows]我應該刪除我的答案並在此處重新發布嗎?' – Cruncher

回答

0

我不知道在您的計算是錯誤的,但你可以做這樣的事情:

編輯的代碼:

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()

+0

線程「main」中的異常java.lang.Error:未解決的編譯問題: \t Sting無法解析爲類型 \t at com.ecsgrid.testC.main(testC.java:34) 當我寫你的代碼,這就是我得到的錯誤 – soupi7

+0

謝謝mkaminsky我解決了它。 – soupi7