方法一:

package com.liaojianya.chapter1;

import java.util.Scanner;

public class SwitchDemo1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter number a : ");
double a = input.nextDouble();
System.out.println("Enter number b : ");
double b = input.nextDouble();
Action ac = new Action(a, b);
ac.command(Action.ADD);
ac.command(Action.SUBTRACT);
ac.command(Action.MULTIPLY);
ac.command(Action.DIVIDE);
ac.command(Action.MOD);
input.close();
}
} class Action
{
double a;
double b;
public Action(double a, double b)
{
this.a = a;
this.b = b;
}
public static final int ADD = 1;
public static final int SUBTRACT = 2;
public static final int MULTIPLY = 3;
public static final int DIVIDE = 4;
public static final int MOD = 5; public void command(int c)
{
switch (c)
{
case 1:
System.out.println(a + " + " + b + " = " + (a + b));
break; case 2:
System.out.println(a + " - " + b + " = " + (a - b));
break; case 3:
System.out.println(a + " * " + b + " = " + (a * b));
break; case 4:
System.out.println(a + " / " + b + " = " + (a / b));
break; case 5:
System.out.println(a + " % " + b + " = " + (a % b));
break; default:
System.out.println("unknown operation!");
break;
}
} }

  方法二:

package com.liaojianya.chapter1;

import java.util.Scanner;

/**
* This program demonstrates the use of switch.
* @author LIAO JIANYA
*
*/
public class SwitchDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); System.out.println("Enter number a : ");
double a = input.nextDouble();
System.out.println("Enter number b : ");
double b = input.nextDouble();
System.out.println("Enter operater :1代表+,2代表-,3代表*,4代表/,5代表% ");
int c = input.nextInt();
switch(c)
{
case 1:
System.out.println(a + " + " + b + " = " + (a + b));
break; case 2:
System.out.println(a + " - " + b + " = " + (a - b));
break; case 3:
System.out.println(a + " * " + b + " = " + (a * b));
break; case 4:
System.out.println(a + " / " + b + " = " + (a / b));
break; case 5:
System.out.println(a + " % " + b + " = " + (a % b));
break; default:
System.out.println("unknown operation!");
break;
}
} }

  运行结果:

Enter number a :
12.3
Enter number b :
32.1
12.3 + 32.1 = 44.400000000000006
12.3 - 32.1 = -19.8
12.3 * 32.1 = 394.83000000000004
12.3 / 32.1 = 0.38317757009345793
12.3 % 32.1 = 12.3

  

switch case实现两个数的算术运算的更多相关文章

  1. 深入理解Java的switch...case...语句

    switch...case...中条件表达式的演进 最早时,只支持int.char.byte.short这样的整型的基本类型或对应的包装类型Integer.Character.Byte.Short常量 ...

  2. c语言基础表达式, 关系运算符, 逻辑运算符, 位运算符, 数据的取值范围, 分支结构(if...else, switch...case)

    1.表达式: 表达式的判断是有无结果(值), 最简单的表达式是一个常量或变量, 如:12, a, 3 + 1, a + b, a + 5 都是表达式 2.BOOL(布尔)数据类型: c语言中除了基本数 ...

  3. c 输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 (MD花了半天时间,思路不对害死人)

    输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 比如: 输入: 输出: 输入: 输出: #include<stdio.h> int main(void) { ...

  4. if else 与switch case判断

    基础数据类型(四类八种 ) 不能为null. 整数型 byte 取值范围2的8次方 short 取值范围2的16次方 int 取值范围2的32次方 一般用int long 取值范围2的64次方 浮点型 ...

  5. 知识扩展--if...else...与switch...case...的执行原理

    一.简述 编程语言中的条件分支结构有两种:if-else和switch-case,这两种条件分支之间可以相互转换,但是也存在一些区别,那么什么时候该用if-else,什么时候该用switch-case ...

  6. Java switch case和数组

    Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句格式: switch(express ...

  7. c语言学习笔记 多级else if 和switch case有什么区别

    ; ) { dosth(); } ) { dosth2(); } else if(opion==) { dosth3(); } else dosth4(); 如果给option的一个值是2的话,那么程 ...

  8. 选择语言之switch case

    程序语言-选择语言之switch   case 多选一,类似if    else if  else if  else 模版: Switch(选择条件) { Case(条件一)//相当于if Conso ...

  9. 使用策略模式重构switch case 代码

    目录 1.背景 2.案例 3.switch…case…方式实现 4.switch…case…带来的问题 5.使用策略模式重构switch…case…代码 6.总结 1.背景 之前在看<重构    ...

随机推荐

  1. 问题-delphi在某电脑(win7)上是界面超乱 DPL

    问题现象:本机运行正常,但是在WIN7或个别的XP上,界面显示控件大小不一,界面超乱 问题原因:可以百度“delphi dpi”或者“delphi 控件自适应分辨率” 问题处理:将窗体的Scaled设 ...

  2. requirejs 打包参数

    https://github.com/requirejs/r.js/blob/master/build/example.build.js

  3. get和post,session和cookie的一些说明

      1.GET和POST的区别 A. 从字面意思和HTTP的规范来看,GET用于获取资源信息而POST是用来更新资源信息. B. GET提交请求的数据实体会放在URL的后面,用?来分割,参数用& ...

  4. Palindrome(poj3974)(manacher算法)

    http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...

  5. js获取浏览器类型

    function add(){ var userAgent = navigator.userAgent, rMsie = /(msie\s|trident.*rv:)([\w.]+)/, rFiref ...

  6. ACE的安装方法

    ACE的安装方法 下载ACE --1  ACE的主页以及下载链接 http://www.cs.wustl.edu/~schmidt/ACE.html 安装ACE --1  将ACE-5.5.tar.g ...

  7. “:Choose a destination with a supported architecture in order to run on this device.”

    我在编译从GitHub上clone下来的<TweeJump>时,出现如下错误:":Choose a destination with a supported architectu ...

  8. mysqldump导出csv格式文件

    mysqldump bstar -t  -T/tmp Nvr  --fields-enclosed-by=\" --fields-terminated-by=, --where=" ...

  9. Java并发——线程安全、线程同步、线程通信

    线程安全 进程间"共享"对象 多个“写”线程同时访问对象. 例:Timer实例的num成员,即add()方法是用的次数.即Timer实例是资源对象. class TestSync ...

  10. python+django+wusgi+nginx安装部署

    基于centos搭建nginx+uwsgi运行django环境 环境: CentOS 7 nginx/1.9.12 Python 2.7.5 一:安装依赖包5 yum install zlib-dev ...