switch case实现两个数的算术运算
方法一:
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实现两个数的算术运算的更多相关文章
- 深入理解Java的switch...case...语句
switch...case...中条件表达式的演进 最早时,只支持int.char.byte.short这样的整型的基本类型或对应的包装类型Integer.Character.Byte.Short常量 ...
- c语言基础表达式, 关系运算符, 逻辑运算符, 位运算符, 数据的取值范围, 分支结构(if...else, switch...case)
1.表达式: 表达式的判断是有无结果(值), 最简单的表达式是一个常量或变量, 如:12, a, 3 + 1, a + b, a + 5 都是表达式 2.BOOL(布尔)数据类型: c语言中除了基本数 ...
- c 输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 (MD花了半天时间,思路不对害死人)
输入两个数,第一个数决定一个nXn的矩阵,第二个数决定从1开始赋值,赋值的上限 比如: 输入: 输出: 输入: 输出: #include<stdio.h> int main(void) { ...
- if else 与switch case判断
基础数据类型(四类八种 ) 不能为null. 整数型 byte 取值范围2的8次方 short 取值范围2的16次方 int 取值范围2的32次方 一般用int long 取值范围2的64次方 浮点型 ...
- 知识扩展--if...else...与switch...case...的执行原理
一.简述 编程语言中的条件分支结构有两种:if-else和switch-case,这两种条件分支之间可以相互转换,但是也存在一些区别,那么什么时候该用if-else,什么时候该用switch-case ...
- Java switch case和数组
Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句格式: switch(express ...
- c语言学习笔记 多级else if 和switch case有什么区别
; ) { dosth(); } ) { dosth2(); } else if(opion==) { dosth3(); } else dosth4(); 如果给option的一个值是2的话,那么程 ...
- 选择语言之switch case
程序语言-选择语言之switch case 多选一,类似if else if else if else 模版: Switch(选择条件) { Case(条件一)//相当于if Conso ...
- 使用策略模式重构switch case 代码
目录 1.背景 2.案例 3.switch…case…方式实现 4.switch…case…带来的问题 5.使用策略模式重构switch…case…代码 6.总结 1.背景 之前在看<重构 ...
随机推荐
- cloudstack安装篇1-linux命令修改IP信息
方式一: ifconfig eth0 192.168.1.18 netmask 255.255.255.0 说明:该种方式可以使改变即时生效,重启后会恢复为原来的IP 方式二: vi ...
- shell脚本应用(3)--语法结构
判断语句 条件判断 test expression [ expression ] 条件表达式中常用的判断 数值-eq -ne -gt -lt -ge -le[equal not greater tha ...
- 不同的jar里边相同的包名类名怎么区别导入
今天在做项目的时候遇到了一个很有意思的问题,折磨了我很长时间,不过最终还是解决了,特留此文纪念一下. 遇到的问题: 同样一段代码,在同事那就好使,在我这就找不到一个方法.引用的包也都是相同的,这种问题 ...
- 编译android版libmpg
环境:ubutnu 12.04,android SDK 1. 下载libmpg的一个android工程,得到一个Android-libmpg-master.zip.https://github.com ...
- A Tour of Go If and else
Variables declared inside an if short statement are also available inside any of the else blocks. pa ...
- Robotium学习笔记一
一. 重签名问题 1.从手机Pull所需的apk通过压缩工具删除META-INF目录 2.通过以下命令行进行签名 >jarsigner -keystore "C:\Documents ...
- MySQL安装配置,命令,异常纪要
一.Mac上的安装配置 // brew安装 brew install mysql // 设置为开机启动 brew services start mysql ...
- IOS 10适配https 包含对于一些http的一些兼容配置
iOS10 从2017年1月1日起苹果提出所有新提交的App默认不允许使用NSAllowsArbitraryLoads来绕过ATS的限制,也就是说强制我们用HTTPS,如果不这样的话提交App可能会被 ...
- AWS s3 python sdk code examples
Yet another easy-to-understand, easy-to-use aws s3 python sdk code examples. github地址:https://github ...
- 六款值得推荐的android(安卓)开源框架简介【转】
http://my.oschina.net/u/1244156/blog/380647 1.volley 项目地址 https://github.com/smanikandan14/Volley-de ...