More than one way to get the max of 3 numbers.
2017-06-01 21:37
519 查看
//需求:键盘录入三个数据获取最大值
下面是初始方法
方法2:
方法3:
方法4:
下面是初始方法
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);
相关文章推荐
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- GetTypes Unable to load one or more of the requested types
- System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the r
- We are unable to complete the review of your app since one or more of your In App Purchases have not
- from one corner to another corner ,please get the shortest path numbers
- The best way to get unique elements of a list in Python
- InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pa
- JBWEB002004: More than the maximum number of request parameters (GET plus POST) for a single request
- Apache Tomcat哈希碰撞拒绝服务漏洞解决办法 or More than the maximum number of request parameters (GET plus POST)
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous
- 反射引发的错误“reflection Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”
- The color chooser class, you could slide the scroll bars to get the specific numbers of the color
- Are you looking for a way to get the entire text of a word document into a RichEdit without using the Clipboard?
- Unable to load one or more of the requested types.
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous. Us
- [转] more than one way to skin an app
- How to get the value of one specified item
- 正则表达式学习指南(十九)----Testing The Same Part of a String for More Than One
- Best way to get the index of an iterator
- 让人哭笑不得的“Unable to load one or more of the types in the assembly”问题的解决!