2013-10-24 2 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이됩니다. 안녕하세요, 4 문자를 추가하면 숫자를 출력하는 간단한 자바 코드를 수행하려고합니다. d-c + a + b와 같이 다른 조합을 사용하면 118.0과 같은 inccorect 번호를 부여합니다. 내 코드에 내 계산이 잘못 어디에 누군가가 말해 줄 수 ..숫자 출력이 잘못된 경우가 있습니다.

가 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); 
} 
} 
+0

이것은 수업 과제가 아니며, 자신을 가르치려고 자바. 고마워요. 도움이된다면, 스마트 한 발언을 자신에게 부탁하십시오. – soupi7

+0

http://stackoverflow.com/questions/19572320/output-incorrect/19572400#19572400 당신이 만든 중복 질문에 대답했습니다. @ [A mod 또는 알고있는 사람] 대답을 삭제하고 여기에 다시 게시해야합니까? – Cruncher

답변

0

나는 당신의 계산이 잘못된 곳 모르겠지만, 당신이 뭔가를 할 수 있습니다 :

편집을 할 CODE :

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 스팅을 유형 으로 해결할 수 없습니다. \t at com.ecsgrid.testC.main (testC.java:34) 내가 코드를 작성할 때 오류가 발생합니다. – soupi7

+0

감사합니다. mkaminsky님께 고맙습니다. – soupi7

관련 문제