简体   繁体   中英

Why is my java calculator code not working?

package calculator;

import java.util.Scanner;

/**
 * @author zhoushi15
 */
public class Calculator {
    public static double num1;
    public static double num2;
    public static String opp;
    /**
     * @param args the command line arguments
     */
    public static double sum;

    public static void main(String[] args) {
        // TODO code application logic here
        boolean quit;
        String calculator;
        String exp;
        System.out.print("Welcome to the AP Computer Science calculator!!");
        Scanner input = new Scanner(System.in);
        boolean calc = false;
        while (calc == false) {
            System.out.print("Enter an expression, or quit to exit: ");
            exp = input.nextLine();
            if (exp.equalsIgnoreCase("quit")) {
                System.out.println("Thanks for stopping by!");
                calc = true;
            } else {
                token(exp);
                System.out.println(exp + "=" + sum);
            }
        }
    }

    public static void token(String x) {
        Scanner jz = new Scanner(x);
        if (jz.hasNextDouble()) {
            if (jz.hasNextDouble()) {
                num1 = jz.nextDouble();
            } else {
                System.out.println("error! It is not a number.");
            }
            if (jz.hasNext()) {
                opp = jz.next();
            }
            if (jz.hasNextDouble()) {
                num2 = jz.nextDouble();
            }
        } else if (jz.hasNext()) {
            if (jz.hasNext()) {
                opp = jz.next();
            }
            if (jz.hasNextDouble()) {
                num1 = jz.nextDouble();
            }
        }
    }

    public static void opp(double num1, String opp, double num2) {
        if (opp.equals("+")) {
            sum = num1 + num2;
        } else if (opp.equals("-")) {
            sum = num1 - num2;
        } else if (opp.equals("*")) {
            sum = num1 + num2;
        } else if (opp.equals("/")) {
            sum = num1 / num2;
        }
    }

    public static void opp2(String opp, double num1) {
        if (opp.equals("|")) {
            sum = Math.abs(num1);
        } else if (opp.equals("v")) {
            sum = Math.sqrt(num1);
        } else if (opp.equals("~")) {
            sum = Math.round(num1);
        } else if (opp.equals("s")) {
            sum = Math.sin(num1);
        } else if (opp.equals("c")) {
            sum = Math.cos(num1);
        } else if (opp.equals("t")) {
            sum = Math.tan(num1);
        }
    }
}

my code is not giving the answer.for example,my input is 4+5,then the output is0.0,but i can't find where's the problem and how to fix it.

Taking a look at your main method, you never assign a value to the variable sum or call a method that would do so. Therefore, your calculator always prints the result as 0.0 which is the default initialized value for doubles. opp and opp2 are not used automatically, you need to actually call them.

Your program has several design issues. Currently the main will call into token(), which sets opp, num1, and num2. Then it returns to main and then main prints 0.

You need to actually do something with num1 and num2. Either have token() call opp1() or opp2() based on the value of opp, or have main call opp1() or opp2() after token().

else {
     token(exp);
     System.out.println(exp + "=" + sum);
}

instead

else{
    token(exp);
    if(opp == "+"){
           sum = opp(num1, opp, num2);
    }
    else{
           sum = opp2(num1, num2);
    }
    System.out.println(exp + "=" + sum);
}

Also for the love of god please rename all your variables and method names. Everything will make more sense to you that way.

As many ppl pointed out - just try to run your program with debugger, then you can see where at least the problems start :)

What ppl have said about sum not being assigned (only initialized) is true and that is a reason why you are getting 0.0 as a result at everything you input.

If you would run with debugger you would notice that jz.hasNextDouble() always return false and subsequently jz.hasNext() returns true and that results in op be the whole expression that you have entered, and at that point you are leaving token method and printing your sum

您永远不会分配sum ghd opp并且opp2方法没有被调用,这就是为什么

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM