Java计算器编程
import java.io.IOException;
import java.util.Scanner;
public class Jisuanqi {
public static void main(String[] args) throws IOException {
opreation op1 = new opreation();
opreation op2 = new opreation();
Scanner read = new Scanner(System.in);
System.out.println("请输入第一个数:");
op1.a = read.nextInt();
System.out.println("请输入运算符:");
char ch = (char) System.in.read();
System.out.println("请输入第二个数:");
op2.b = read.nextInt();
char c=ch;
switch (c) {
case '+':
add ad = new add();
ad.add(op1.a, op2.b);
break;
case '-':
sub su = new sub();
su.sub(op1.a, op2.b);
break;
case '*':
mul mu = new mul();
mu.mul(op1.a, op2.b);
break;
case '/':
div di = new div();
di.div(op1.a, op2.b);
break;
}
}
}
class opreation{
int a,b,c;
void add(){
}
void sub(){
}
void mul(){
}
void div(){
}
}
class add extends opreation{
public void add(int a,int b){
c=a+b;
System.out.println(a+"+"+b+"="+c);
}
}
class sub extends opreation{
public void sub(int a,int b){
c=a-b;
System.out.println(a+"-"+b+"="+c);
}
}
class mul extends opreation{
public void mul(int a,int b){
c=a*b;
System.out.println(a+"*"+b+"="+c);
}
}
class div extends opreation{
public void div(int a,int b){
if(b==0)
System.out.println("除数不能为0");
double c=a/b;
System.out.println(a+"/"+b+"="+c);
}
}