import java.util.*;
public class Test1
{
public static void main(String[] args)
{
// int a = 10;
// int b = 23;
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int a = input.nextInt();
System.out.println("请再输入一个数:");
int b = input.nextInt();
char[] ch = {'+','-','*','/'};
char c = ch[0];
{
for(int i = 0; i < ch.length; i ++)
{
Random ran = new Random();
c = (char) ran.nextInt(ch.length);
}
}
Operation op = OperationFactory.getInstance(c);
int result = op.oper(a,b);
System.out.println(""+a+c+b+" ="+result);
}
}
public class OperationFactory
{
public static Operation getInstance(char c)
{
Operation op = null;
switch(c)
{
case '+' : op = new Add();break;
case '-' : op = new Sub();break;
case '*' : op = new Mul();break;
case '/' : op = new Div();break;
}
return op;
}
}
public class Operation
{
int oper(int a ,int b)
{
return 0;
}
}
class Add extends Operation
{
int oper (int a , int b)
{
return a + b;
}
}
class Div extends Operation
{
int oper (int a , int b)
{
return a / b;
}
}
class Sub extends Operation
{
int oper (int a , int b)
{
return a - b;
}
}
class Mul extends Operation
{
int oper (int a , int b)
{
return a * b;
}
} |
|