【问题标题】:CommandLine Java Calculator miscalculates命令行 Java 计算器计算错误
【发布时间】:2011-10-31 16:02:34
【问题描述】:

我刚刚学习了 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, 而这显然是不正确的。

【问题讨论】:

标签: java calculator


【解决方案1】:

您使用的是哪个 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);

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

【讨论】:

  • 那个循环还有另一个问题(用 12+12 检查)
  • ...运算符优先级,空格,负数,...
  • 但通常第一个程序是计算器,这是很好的数学+gui+逻辑练习(如果你是用 UI 创建,则练习检查数据类型,解析用户输入等)
  • 我以前有编程经验,我喜欢做复杂的事情。因为很容易让它像询问 3 次 first num second num 运算符。制作这个程序很有趣,但另一个很无聊。您的示例是否适用于 27 + 3 之间的更多 pcase?
【解决方案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
}

开始索引上也有一个减一(这就是+1int temp = operatorloc+1 中的来源)

【讨论】:

  • 感谢您帮助解决了我的问题,您能解释一下真正的问题吗
【解决方案3】:

您的逻辑在这里有些无聊:

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

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

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      您的 snum 分配错误。 现在因为它看起来像家庭作业:) 我不会把它拼出来。

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

      【讨论】:

      • 这不是家庭作业 :P 我正在为自己挑战一个人学习 java。
      【解决方案6】:

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

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

      【讨论】:

        【解决方案7】:

        这里我有一个简单但总是正确计算的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();
            }
        
        }
        

        调用程序并查看结果

        【讨论】:

          猜你喜欢
          • 2011-10-31
          • 2018-04-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-27
          • 2015-09-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多