More than one way to get the max of 3 numbers.
来源:互联网 发布:淘宝旺旺名怎么设置 编辑:程序博客网 时间:2024/05/18 11:25
//需求:键盘录入三个数据获取最大值
下面是初始方法
public static void main(String[] args) { // prompt user to enter number Scanner sc = new Scanner(System.in); System.out.println("Please enter num 1 :"); int a = sc.nextInt(); System.out.println("Please enter num 2 :"); int b = sc.nextInt(); System.out.println("Please enter num 3 :"); int c = sc.nextInt(); // call the fuction getMax(a, b, c); } private static void getMax(int a, int b, int c) { // 方法1:if,比武 int max = a; if (a < b) { max = b; } if (max < c) { max = c; } System.out.println("The max num = " + max); }
方法2:
int max; if (a > b) { if (a > c) { max = a; } else { max = c; } } else { if (b > c) { max = b; } else { max = c; } }
方法3:
// 方法2: 三元运算法,两行代码int temp = (a > b) ? a : (b);int max = (temp > c) ? temp : c;
方法4:
// 方法3: 三元运算法,一行代码int max = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
阅读全文
0 0
- More than one way to get the max of 3 numbers.
- More than one way to get gcd(greatest common divisor,最大公约数)
- One way to join the string together more faster
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Result consisted of more than one row
- Result consisted of more than one row
- 正则表达式学习指南(十九)----Testing The Same Part of a String for More Than One
- The Wrong Way (and One Right Way) to Upgrade Your Phone at Your Own Pace Read more: http://techland
- JBWEB002004: More than the maximum number of request parameters (GET plus POST) for a single request
- How to get the default value in the paramter block description of 3ds max
- django入门:get() returned more than one topic
- How to get the value of one specified item
- How to get the tables of one schema?
- get the nsum of 'Fibonacci'from one to twenty
- ERROR: Running more than one instance of Hudson
- Result consisted of more than one row 错误的解决
- Result consisted of more than one row错误的修正
- Mysql报错:Result consisted of more than one row
- 39 C语言变量的定义位置以及初始值
- HDU1874畅通工程续
- Android 6.0新控件 BottomSheetDialog | 底部对话框 介绍及使用详情
- springmvc学习记录-20170601
- Spring 与 SpringMVC 父子容器问题
- More than one way to get the max of 3 numbers.
- JVM、Dalvik以及ART的区别
- USACO-Section1.3 Combination Lock [其他]
- 向链表添加节点数据
- ORB_SLAM 编译过程详解
- c++获取cpu逻辑核心数
- Javascript中的delete介绍
- 无限轮播
- 文章标题