0
点赞
收藏
分享

微信扫一扫

java笔试题目——要求:仅打印出a=100,b=200,请写出method方法的代码


//题目:

public class Test {
    

    public static void main (String[] args){

        int a = 10;
        int b = 10;
        method(a,b);   //需要在method方法被调用之后,仅打印出a=100,b=200,请写出method方法的代码。

        System.out.println("a="+a);
        System.out.println("b="+b);

    }
    //代码编写处




}

方法一:

public static void method(int a,int b){

//在不改变原题目的情况下如何写这个函数才能在main 函数中输出a=100,b=200?
    a= a*10;
    b= b*20;

    System.out.println(a);
    System.out.println(b);
    System.exit(0);
}







//      解析  static void              exit (int status)___Terminates the currently running java Virtual Machine            输入0_____终止当前jvm的执行

方法二:

public static void method ( int a ,int b){
    

//把System.out.println()打印流重置

    PrintStream ps  = new PrintStream(System.out){
        @Override    
    
    public void println(String x){
        if("a=10".equals(x)){
            x="a=100";
        }
        else if("b=10".equals(x)){
            X= "b=200";
        }
         super.println(x);
    }


  };
System.setOUt(ps);


}

 

题目二:

 

java笔试题目——要求:仅打印出a=100,b=200,请写出method方法的代码_System

 

 

 

题目三:

int[] arr = new int[10];
System.out.println(arr);           //这里该输出什么?           大多数人答案是: 地址值

char[] arr1 = new char[10];
System.out.println(arr1);          //这里该输出什么?            大多数人的答案是:地址值

 

//做个demo

public class ArrayPrintTest{
    
    public static void main (String[] args) {
        int arr =new int[]{1,2,3};
        System.out.println(arr);//输出的确实是地址值

        char[] arr1  = new char[]{'a','b','c'};
        System.out.println(arr1);//输出的是abc
    }
}

诡异的是,如果错了,面试官对你说了一句:你回去看看,

java笔试题目——要求:仅打印出a=100,b=200,请写出method方法的代码_bug调试_02

看?这时候看什么?书本都是按章节讲话,无从下手???是啊是啊/

 

用debug调试。

第一个输出用的是Object方法

第二个用的是用的是char类型,根本不是方法,当要输出方法体的时候,会给你遍历数组。

java笔试题目——要求:仅打印出a=100,b=200,请写出method方法的代码_java_03

java笔试题目——要求:仅打印出a=100,b=200,请写出method方法的代码_bug调试_04

举报

相关推荐

0 条评论