用java编写一个简单计算器只有加减乘除就行,在控制台输入谁加谁得谁就行 25
1个回答
展开全部
import java.util.*;
public class TestSum {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while(true){
init();
}
}
private static void init() {
System.out.println("请输入算数表达式如:1+1回车即可:");
String str = sc.nextLine();
if (!str.matches("[^a-zA-Z()&^#$@!~]+")) {
System.out.println("输入错误重输:请输入算数表达式如:1+1回车即可:");
str = sc.nextLine();
}
int q = 0, h = 0, sum = 0;
char chs = ' ';
for (int i = 0; i < str.length(); i++) {
chs = str.charAt(i);
if (chs == '+' || chs == '-' || chs == '*' || chs == '/') {
q = Integer.valueOf(str.substring(0, i));
h = Integer.valueOf(str.substring(i + 1, str.length()));
switch (chs) {
case '+':
sum = q + h;
break;
case '-':
sum = q - h;
break;
case '*':
sum = q * h;
break;
default:
sum = q / h;
break;
}
break;
}
}
System.out.println("结果="+sum+"\n继续下一轮:");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询