我有一些条件:
我做到了,但是有一些问题..
问题是,当我首先输入命令行:127 + 3
时,它很好,但是当我输入-
时,它停止并说:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Calculator2.main(Calculator2.java:14)
我的代码:
import java.util.*;
import java.io.*; //just for insurance
public class Calculator2 {
public static void main(String[] args) throws Exception {
System.out.println("Welcome to the Best Calculator");
Scanner sc = new Scanner(System.in);
double first, second, ans = 0;
char operand;
while (true) {
first = sc.nextDouble();
operand = sc.next().charAt(0);
second = sc.nextDouble();
if (operand != '=') {
switch (operand) {
case '+':
ans += (first + second);
break;
case '-':
ans -= (first - second);
break;
}
} else if (operand == '=') {
System.out.println("The result is: " + ans);
System.exit(0);
}
}
}
}
一些例子:
欢迎使用最佳计算器
127 + 3
-
40
=
结果是:90
需要帮助,我希望有很多好人)