【问题标题】:Calculator bug only outputting equation not answer计算器错误只输出方程不回答
【发布时间】:2018-01-05 09:57:52
【问题描述】:

当代码运行并输入输入时,应该解决方程的输出只打印先前输入的方程。请注意,代码中的注释在很大程度上是不正确的,因为它们来自代码的早期迭代。

public static void main(String[] args){

    // introduces the user to the programme
    System.out.println("Welcome to my calculator test");
    System.out.println("Function inputs; + to add numbers, - to subtract numbers, * to multiply, / to devide, ^ to power,# to square root.");

    //creates a Scanner
    Scanner reader0 = new Scanner(System.in);
    //promts the user for an input
    System.out.println("Please enter first number"); 
    String a = reader0.nextLine();
    reader0.close();
    char test = 'x';

    String test1 = "";
    for (int i = 0; i < a.length (); i++ ){
        test = a.charAt(i);
        if (Character.isDigit(test) || test == ('.')){
            System.out.println(test);
            test1 = test1+""+test;
        }
        else if (test == '+' ||test == '-' ||test == '*' ||test == '/' ||test == '#' ||test == '^'){
            char b = a.charAt(i);
        }
    }
    System.out.println(test);
    System.out.println(test1);
    double number = Double.parseDouble(test1);

    // checks that if the user intends to squareroot the first number
    if (test == '#'){
        // square roots the first number
        System.out.println(java.lang.Math.sqrt(a));
    }
    else{

        //creates a Scanner
        Scanner reader2 = new Scanner(System.in);
        //promts the user for an input
        System.out.println("Please enter Second number");
        String c = reader2.nextLine();
        reader2.close();

        // checks if user wanted to add
        if (test == '+'){
            // adds the two numbers
            System.out.println(a+c);
        }
        //checks if user wanted to subtract
        else if (test == '-'){
            // subtracts the two numbers
            //System.out.println(a-c);
        }
        //checks if user wanted to multiply
        else if (test == '*'){
            // multiplies the two numbers
            //System.out.println(a*c);
        }
        //checks if the user wanted to divide
        else if (test == '/'){
            // devides the two numbers
            //System.out.println(a/c);
        }
        //checks if the user wants to power the numbers
        else if (test == '^'){
            // powers the first number by the second number
            //System.out.println(java.lang.Math.pow(a,c));
        }
        // if the programme is unsure what the user intended then a error message will be printed to screen
        else{
            System.out.println("ERROR.UserInputForFunctionVoid");
        }
    }
}

对于解决此问题的任何帮助将不胜感激。

谢谢。

【问题讨论】:

标签: java debugging calculator


【解决方案1】:

首先,这不会编译为Math.sqrt(a),因为String 不能像这样转换为double。即使adouble,我也不认为它会按预期工作。

您最好首先阅读intdouble,例如double a = reader0.nextDouble();

您可以通过读入第一个数字,然后读入运算符来稍微缩短您的代码。然后检查是否需要平方根(因为我猜你在这个阶段不需要第二个数字),然后询问第二个数字。

我也会先声明我的结果 (double result = 0.0;),然后在最后打印出来。

所以在你检查了#之后,把剩下的放在else中:

if (operator.equals("+")) {
            result = a + b;
        } else if (operator.equals("-")) {
            result = a - b;
        } 
        //and so on...

注意:您的结果可能会在小数点后有 x 位数,因此不妨考虑使用 String.format("%.2f", result) 或类似的东西。

额外说明:您也可以考虑使用switch 代替if...else if

这是partial demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多