CommandLine Java计算器计算错误

时间:2011-08-21 17:01:36

标签: java calculator

我刚刚学习了java并询问了一个关于我的单行计算器的问题,它不再给出错误,但它计算错误。 这是代码

import java.util.Scanner;

public class omg {
    public static void main(String args[]) {
        int fnum,snum,anum = 0;
        String strtype;
        char[] testchar;
        char currentchar;
        int machinecode = 0;
        String tempnumstr;
        int operatorloc = 0;
        char[] tempnum = new char[256]; 
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter The Calculation: ");
        strtype = scan.nextLine();
        testchar = strtype.toCharArray();
        for(int b = 0; b < testchar.length; b++)
        {
            currentchar = testchar[b];
            if(currentchar == '+') {
                machinecode = 1;
                operatorloc = b;
            }
            else if(currentchar == '-') {
                machinecode = 2;
                operatorloc = b;
            }
            else if(currentchar == '*') {
                machinecode = 3;
                operatorloc = b;
            }
            else if(currentchar == '/') {
                machinecode = 4;
                operatorloc = b;
            }
        }
        for(int t = 0;t < operatorloc;t++) {
            tempnum[t] = testchar[t];
        }
            tempnumstr = new String(tempnum).trim();
            fnum = Integer.parseInt(tempnumstr);
        for(int temp = operatorloc;temp < testchar.length;temp++) {
            for(int t = 0;t<(testchar.length-operatorloc);t++) {
                tempnum[t] = testchar[temp];
            }
        }
        tempnumstr = new String(tempnum).trim();
        snum = Integer.parseInt(tempnumstr);
        switch(machinecode) {
        case 1:
            anum = fnum + snum;
            break;
        case 2:
            anum = fnum - snum;
            break;
        case 3:
            anum = fnum * snum;
            break;
        case 4:
            anum = fnum / snum;
        }
        System.out.println(anum);
    }
}

这段代码会给我8 + 8 = 96, 这显然不正确。

7 个答案:

答案 0 :(得分:3)

您使用的是哪个IDE?日食?在这种情况下,您应该尝试调试视角

我简单介绍了你的程序,发现了以下内容:

在这一行:

tempnumstr = new String(tempnum).trim();

tempnum等于{ 8, 8 }temunumstr变为88(这就是为什么你得到88 + 8 = 96)

我必须说程序有点混乱,而不是创建计算器的方法。你真的把一项复杂的任务作为第一次练习; - )

那就是说,我认为你应该改变

for (int temp = operatorloc; temp < testchar.length; temp++) {
    for (int t = 0; t < (testchar.length - operatorloc); t++) {
        tempnum[t] = testchar[temp];
    }
}
tempnumstr = new String(tempnum).trim();

只有一行

tempnumstr = strtype.substring(operatorloc + 1);

(至少它会为输入16提供8+8。)

答案 1 :(得分:2)

第二个学期的循环是错误的

您正在嵌套循环,将其更改为

for(int temp = operatorloc+1, t=0 ;temp < testchar.length;temp++, t++ ) {
    tempnum[t] = testchar[temp];
}

它会起作用

编辑:

原始循环

for(int temp = operatorloc;temp < testchar.length;temp++) {
    for(int t = 0;t<(testchar.length-operatorloc);t++) {
        tempnum[t] = testchar[temp];
    }
}

基本上等同于

int temp = testchar.length-1;
for(int t = 0;t<(testchar.length-operatorloc);t++) {
    tempnum[t] = testchar[temp];//temp doesn't change
}

并且在起始索引上也有一个关闭(+1来自int temp = operatorloc+1

答案 2 :(得分:2)

你的逻辑在这里有点蠢蠢欲动:

  for (int temp = operatorloc; temp < testchar.length; temp++) {
     for (int t = 0; t < (testchar.length - operatorloc); t++) {
        tempnum[t] = testchar[temp];
     }
  }

要查看它正在做什么,添加一个println用于调试puroses:

  for (int temp = operatorloc; temp < testchar.length; temp++) {
     for (int t = 0; t < (testchar.length - operatorloc); t++) {
        tempnum[t] = testchar[temp];
        System.out.println("test: " + new String(tempnum).trim());
     }
  }

答案 3 :(得分:1)

尝试在计算之前打印fnumsnum,您会看到snum的值为88.您的代码对我来说并不明显,所以您应该调试它并手动修复:)

答案 4 :(得分:1)

你的snum分配是错误的。 现在,因为它看起来像家庭作业:)我不打算拼写出来。

您还需要了解一些Java命名约定,因为它会使代码更具可读性。 最后一个建议是使用常量来表示机器代码。

答案 5 :(得分:1)

你应该下载一个IDE(eclipse Netbeans和IntelliJ都是免费的或有免费版本),并在他们的调试器中执行你的程序。您很快就会发现错误。

也就是说,停止像字符串数组一样处理字符串,并停止使用固定长度数组,希望它们的大小足够。 String是一个成熟的对象,有很多有用的方法。 substring是其中之一,可用于提取左右操作数。与从一个char数组复制到另一个char数组,重用,一个(使用tempnum存储两个操作数的字符,而不将cahrs重置为默认值)会更容易,也更少错误。

答案 6 :(得分:0)

这里我有一个简单而简单但总能正确计算的Java计算器:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {

    public static void main(String[] args) {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("javascript");

    try {
        Object o = engine.eval(allArgs(args));
        System.out.println("The result is = " + o.toString());
    } catch (ScriptException e) {
        System.out.println("Please type in a syntactically correct expression.");
    }
    }

    private static String allArgs(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (String s : args) {
        sb.append(s);
    }
    return sb.toString();
    }

}

调用程序并查看结果