switch case实现两个数的算术运算
来源:互联网 发布:金融网络销售做不得 编辑:程序博客网 时间:2024/06/01 11:59
方法一:
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.3Enter number b : 32.112.3 + 32.1 = 44.40000000000000612.3 - 32.1 = -19.812.3 * 32.1 = 394.8300000000000412.3 / 32.1 = 0.3831775700934579312.3 % 32.1 = 12.3
1 0
- switch case实现两个数的算术运算
- 用switch-case语句实现两个数的简单四则运算
- 不用算术运算符实现两个数的加法操作
- 不用算术运算符实现两个数的加法
- 不用算术运算符实现两个数的加法。
- 不用算术运算符实现两个数的加法(按位异或)
- 不用算术运算符实现两个数的加法(按位异或)
- 不用算术运算符实现两个数的加法(按位异或)
- 不用算术运算符实现两个数的加法(按位异或)
- 函数实现两个数的和,不能用+等算术运算符
- [面试题] 不用算术运算符实现两个数的加法
- 不用任何算术运算符计算两个数的和
- 每日一题:编写一个函数,不使用算术运算符,实现比较两个数的大小
- 位运算实现两个数的加法
- 用switch控制运算符,输入两个数,计算他们的运算结果;
- 两个数的运算。。。
- 使用map实现函数的switch-case
- Java的if、switch...case控制语句 求水仙花数
- session入库
- centos长ping输出日志的脚本
- hdu 5754 Life Winner Bo
- structs2 json java jsp 下载单个文件
- 喵星人与汪星人
- switch case实现两个数的算术运算
- 老菜鸟迈出的第一步——Android调用WebService碰到的种种问题
- 实现web消息推送
- csdn的第一天
- 【POJ】[2503]Babelfish
- 最长公共子串
- Linux CAN驱动及测试总结
- HDU5726 GCD 二分查找加RMQ 多校联赛第一场
- 排序算法的研究