简单计算器
作者:互联网
简单计算器
仅用于两个数之间的运算。
//简单计算器
public static void main(String[] args) {
while (true) {
System.out.println("输入第一个数:");
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
System.out.println("输入运输符号:");
Scanner scanner1 = new Scanner(System.in);
char symbol = scanner.next().charAt(0);
System.out.println("输入第二个数:");
Scanner scanner2 = new Scanner(System.in);
int b = scanner.nextInt();
if (symbol!='+' && symbol!='-' && symbol!='*' && symbol!='/') {
System.out.println("无效运算符!");
} else {
System.out.println("结果是:" + fuhao(a, b, symbol));
}
}
}
//加法
public static int add(int a,int b){
return a+b;
}
//减法
public static int sub(int a,int b){
return a-b;
}
//乘法
public static int mul(int a,int b){
return a*b;
}
//除法
public static int div(int a,int b){
return a*b;
}
//运算符判断
public static int fuhao(int a,int b,char ch) {
int sum=0;
switch (ch) {
case '+':
sum=add(a, b);
break;
case '-':
sum =sub(a, b);
break;
case '*':
sum=mul(a, b);
break;
case '/':
sum=div(a, b);
break;
}
return sum;
}
标签:int,sum,计算器,System,public,static,简单,symbol 来源: https://www.cnblogs.com/qinyu33/p/16579624.html