计算器错误只输出方程式而不是答案

时间:2018-01-05 09:57:52

标签: java debugging calculator

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

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

非常感谢您解决此问题的任何帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

首先,这不会编译为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

相关问题