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);
}
}
}
我的代码没有给出答案。例如,我的输入是 4+5,然后输出是 0.0,但我找不到问题出在哪里以及如何解决它。