第十章 面向对象 简答题2

package cn.jbit.test.yang;

public class Calculator {
	// 计算器类
	public int num1;
	public int num2;
	public int sum;
	public char fuhao;

	public void show() {
		sum = 0;
		switch (fuhao) {
		case '+':
			sum = num1 + num2;
			break;
		case '-':
			sum = num1 - num2;
			break;
		case '*':
			sum = num1 * num2;
			break;
		case '/':
			sum = num1 / num2;
			break;
		default:
			System.out.println("输入错误");
		}
		System.out.println(sum);

	}
}


 

package cn.jbit.test.yang;

import java.util.Scanner;

public class Calculator1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Calculator calculator = new Calculator();
		System.out.print("请输入第一个数");
		calculator.num1 = input.nextInt();
		System.out.print("请输入第二个数");
		calculator.num2 = input.nextInt();
		System.out.print("请输入运算符");
		calculator.fuhao = input.next().charAt(0);
		calculator.show();
	}
}


 

你可能感兴趣的:(第十章 面向对象 简答题2)