1、 不能使用boolean,if else等判断语句,要求求出两个数中的大数
之前在《程序员宝典》这本书中看到了相关的两种解法,分别为采用数学方法与移位方法。 今天在这里提供一种新的思路:
采用java中的异常来解决这个问题:
- public static int getMaxValueWidthoutBoolean(int a,int b){
- int[] array=new int[a];
- try{
- //只要b比a大,数组越界,肯定会导致异常,否则b比a小
- //所以在catch模块中返回b,非异常返回a即可
- array[b]=0;
- }catch(Exception ex){
- return b;
- }
- return a;
- }