Discuss / Java / 同学们做题了

同学们做题了

Topic source

🌙

#1 Created at ... [Delete] [Delete and Lock User]

以下代码执行的结果显示是多少()?

public class Demo{
    public static void main(String[] args){
        System.out.print(getNumber(0));
        System.out.print(getNumber(1));
        System.out.print(getNumber(2));
        System.out.print(getNumber(4));
    }



    public static int getNumber(int num){
        try{
            int result = 2 / num;
            return result;
        }catch (Exception exception){
            return 0;
        }finally{
            if(num == 0){
                return -1;
            }
            if(num == 1){
                return 1;
            }
        }
    }    
}

Fwmmmm-

#2 Created at ... [Delete] [Delete and Lock User]

-1,1,1,0

-1110

Joker.fu_95

#4 Created at ... [Delete] [Delete and Lock User]

无论有无异常,finally都是最后执行,如果finally也有return就会将前面的return结果覆盖


  • 1

Reply