import java.util.Scanner;
//写一个计算机,要求实现加减乘除功能,并且能够循环接收新的数据,通过用户交互来实现
public class Demo08 {
public static void main(String[] args) {
int a=0;
int b=0;
char s='\u0000';
Demo08 demo08 = new Demo08();
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入第一个操作数:");
if(scanner1.hasNextInt()){
a = scanner1.nextInt();
}
Scanner scanner2 = new Scanner(System.in);
System.out.println("请输入第二个操作数:");
if(scanner2.hasNextInt()){
b = scanner2.nextInt();
}
Scanner scanner3 = new Scanner(System.in);
System.out.println("请输入操作符:");
if(scanner3.hasNextLine()){
s = scanner3.next().charAt(0);
demo08.cal(a,b,s);
}
scanner1.close();
scanner2.close();
scanner3.close();
}
int a;
int b;
//计算
public void cal(int a,int b,char s){
switch(s){
case '+':
System.out.println(plus(a,b));
break;
case '-':
System.out.println(sub(a,b));
break;
case '*':
System.out.println(mul(a,b));
break;
case '/':
System.out.println(exc(a,b));
break;
}
}
//加
public int plus(int a,int b){
return a+b;
}
//减
public int sub(int a, int b){
if(a>=b){
return a-b;
}else{
return b-a;
}
}
//乘
public int mul(int a,int b){
return a*b;
}
//除
public int exc(int a,int b){
if(b==0){
System.out.println("除数不能为0");
return -1;
}else{
return a/b;
}
}
}
```
标签:Java,Scanner,实现,System,int,println,out,public,加减乘除
From: https://www.cnblogs.com/qieyun/p/18100387