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);
}
}
}
내 코드 내 입력, 출력 is0.0 4 + 5,하지만 문제 및 해결 방법을하는 어디 내가 찾을 수 있으며, answer.for 예를 포기하지 않을 입니다.왜 내 Java 계산기 코드가 작동하지 않습니까?
* 들여 쓰기를 수정하십시오. – NPE
디버거로 코드를 실행하십시오. – dbf
테스트에서 hasNextDouble()이 두 번 연속으로 테스트 되었습니까? – kviiri