/*使用java类描述一个计算机类
*
*/
class Calculator
{
private int num1;
private int num2;
private char option;
//
public void initCalculator(int n1, int n2, char o){
num1 = n1;
num2 = n2;
if(o == '*' || o == '-' || o == '/' || 0 == '+')
{
option = o;
}
else{
option = '+';
}
}
public void calculate() // 运算
{
switch(option){
case '+':
System.out.println("做加法运算,结果是" + (num1+num2));
break;
case '-':
System.out.println("做减法运算,结果是" + (num1-num2));
break;
case '*':
System.out.println("做乘法运算,结果是" + (num1*num2));
break;
case '/':
System.out.println("做除法运算,结果是" + (num1/num2));
break;
}
}
}
class Demo20
{
public static void main(String[] args)
{
Calculator c = new Calculator();
c.initCalculator(1,2,'+');
c.calculate();
}
}