java代码实例 使用switch实现简易的计算器(实现加减乘除)

import java.util.Scanner;

/*
 * 使用switch实现简易的计算器(实现加减乘除); 
 */
public class test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入一个表达式(例如: 5 + 3):");
		int a = sc.nextInt();
		char ch = sc.next().charAt(0);
		int b = sc.nextInt();
		switch (ch) {
		case '+':
			jia(a, b);
			break;
		case '-':
			jian(a, b);
			break;
		case '*':
			cheng(a, b);
			break;
		case '/':
			chu(a, b);
			break;
		default:
			System.out.println("输入错误");
			break;
		}
	}

	private static void cheng(int a, int b) {
		int sum = a * b;
		System.out.println("两数积:" + sum);
	}

	private static void jian(int a, int b) {
		int sum = a - b;
		System.out.println("两数差:" + sum);
	}

	private static void chu(int a, int b) {
		if (b == 0) {
			System.out.println("除数不能为零~");
		} else {
			int sum = a / b;
			System.out.println("两数商:" + sum);
		}
	}
	private static void jia(int a, int b) {
		int sum = a + b;
		System.out.println("两数和:" + sum);
	}
}

posted @ 2013-05-22 16:08  一个学渣  阅读(7416)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved