Output incorrect [duplicate]

2019-09-25 00:51发布

问题:

This question is an exact duplicate of:

  • Output of numbers are incorrect sometimes. 1 answer

When I do different combinations like d-c+a+b it gives me a inccorect number like 118.0. Can someone tell me where in my code my calculations are wrong.. Thank you

the ValVarPairs.txt contains these numbers-> a=100,b=5,c=10,d=13 This is what i coded.

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);  
 }
}

回答1:

Let's debug this a little, add a few system outs...

this is what you would see for each step 100.0 - 5.0 95.0 + 10.0 105.0 + 13.0 118.0

your value array is {100,5,10,13} and your operator array is {-,+,+}

you have not mapped a = 100, b = 5, c= 10, d = 13, unless you map those then parse the operands using the mapping based on the non operand input keys, it is not going to work.

So, if I were to use the character's int values, I would be able to translate it this way.

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, operatorsAndOperands[] = new char[3];
        for (i = 0; i <= 2; i++)
            operatorsAndOperands[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;
            }
            for (int l = 0; l < values.length; l++) {
                System.out.println(values[l]);
            }
        } 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().toUpperCase();
        } catch (IOException f) {
            System.out.println(f);
            return;
        }

        if(InputText.length() > 0){
            operatorsAndOperands = new char[InputText.length()];
        } else {
            System.out.println("No Operations specified");
            return;
        }
        for (i = 0; i < InputText.length(); i++) {
            k = InputText.charAt(i);
            operatorsAndOperands[j++] = k;
        }

        result = 0; 
        for (i = 0; i < operatorsAndOperands.length; i++) {
            System.out.println(operatorsAndOperands[i] + " " + (int)operatorsAndOperands[i]);
            if(i+1<operatorsAndOperands.length)
                System.out.println(operatorsAndOperands[i+1]);
            switch(operatorsAndOperands[i]){
            case '+':
                if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){
                    result+=values[(int)operatorsAndOperands[i+1] - (int)'A'];
                    i++;
                }
                break;
            case '-':
                if(operatorsAndOperands[i+1] != '+' && operatorsAndOperands[i+1] != '-'){
                    result-=values[(int)operatorsAndOperands[i+1] - (int)'A'];
                    i++;
                }
                break;
            default:
                result = values[(int)operatorsAndOperands[i] - (int)'A'];
                break;
            };
            System.out.println(result);
        }
        System.out.println(result);
    }
}


回答2:

Right now you would be getting the same output if your input was -++

You never parse for the order or a, b, c and d. You always assume the order a->b->c->d.

So d-c+a+b will be: a-b+c+d which is consistent with the output you provided(100-5+10+13 = 118)

OP's CODE

  for (i = 0; i < InputText.length(); i++)
  {
     k = InputText.charAt(i);
     if ((k == '+') || (k == '-'))
     {
        if (j <= 2) operators[j++] = k;   
     }
  } 

/OP'S CODE

In this loop, when k is not an operator, you should be reading which letter it is, and store the order in which the letters appeared. Or build some other kind of mapping. In any case you can't just ignore non-operator characters because they are part of the input.



标签: java parsing