输出不正确 [英] Output incorrect

查看:95
本文介绍了输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进行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);  
 }
}

解决方案

让我们调试一下,添加一些系统输出...

这是您在每个步骤中看到的内容 100.0-5.0 95.0 + 10.0 105.0 + 13.0 118.0

您的值数组为{100,5,10,13},而您的运算符数组为{-,+,+}

您尚未映射a = 100,b = 5,c = 10,d = 13,除非您进行映射,然后使用基于非操作数输入键的映射来解析操作数,否则它将无法正常工作. /p>

因此,如果我要使用字符的int值,则可以通过这种方式进行翻译.

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

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

解决方案

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

这篇关于输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆