有时输出数字不正确。 [英] Output of numbers are incorrect sometimes.
问题描述
这是我的任务,给定一个表达式的输入,该表达式包含一串字母和运算符(加号,减号和字母.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这样的内含号。有人能告诉我在我的代码中我的计算错误..
This is my task, Given an input of an expression consisting of a string of letters and operators (plus sign, minus sign, and letters. IE: ‘b-d+e-f’) and a file with a set of variable/value pairs separated by commas (i.e: a=1,b=7,c=3,d=14) write a program that would output the result of the inputted expression. For example, if the expression input was ("a + b+c -d") and the file input was ( a=1,b=7,c=3,d=14) the output would be -3. Hi I am trying to do a simple java code which outputs a number if i add 4 letters. 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..
谢谢
ValVarPairs.txt包含这些数字 - > a = 100,b = 5,c = 10,d = 13
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);
}
}
推荐答案
我我不确定你的计算错误,但你可以这样做:
I'm not sure where your calculations are wrong, but you could do something like this:
编辑代码:
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()
There are some problems in your code where you use "=="
instead of .equals()
这篇关于有时输出数字不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!