zoukankan      html  css  js  c++  java
  • 程序题

    输出的结果:

    问题1.

    public class Test1 {
       public static void changeStr(String str){
           str ="welcome";
       }
       public static void main(String args[]){
           String str="1234";
           changeStr(str);
           System.out.println(str);
       }
    }

    //输出结果:1234
    //这里虽然是一个静态方法,但是里面的变量是一个局部变量,
    //所以这里不因为是静态方法,就误认为里面的变量也是静态变量了

    问题2.

    public class Test2 {
        static boolean foo(char c){
            System.out.println(c);
            return true;
        }
        public static void main(String[] args) {
            int i=0;
            for(foo('A');foo('B')&&(i<2);foo('C')){
                i++;
                foo('D');
            }
        }
    }

    What is the result?
    A. ABDCBDCB
    B. ABCDABCD
    C. Compilation fails.
    D. An exception is thrown at runtime.

    输出结果是:ABDCBDCB
    分析:FOR循环里面讲究的条件要为真,与你的判断式是什么没有关系
    就像这里,虽然是打印的字母,但是却不是false,所以可以执行
    第一次进行循环:
    foo('A')打印字母A,(注:这里不是false条件就默认为true条件)
    foo('B')打印字母B,i=0,比较(i < 2),条件为true,进行循环体,foo('D')打印D
    foo('C')打印字母C
    第二次循环:
    foo('B')打印B,i=1,比较(i < 2)为true,进行循环体,foo('D')打印D
    foo('C')打印字母C
    第三次循环:
    foo('B')打印字母B,i=2,比较(i < 2)为false,退出循环,得结果

    问题3.

    public class Test3 {
        String str = new String("good");
        char[] ch = { 'a', 'b', 'c' };
    
        public static void main(String args[]) {
            Test3 ex = new Test3();
            ex.change(ex.str, ex.ch); 
            System.out.print(ex.str + " and ");
            System.out.print(ex.ch);
        }
    
        public void change(String str, char ch[]) {
            str = "test ok";
            ch[0] = 'g';
        }
    }

    输出结果:good and gbc

    // (ex.str,ex.ch被当作参数,所以下面的赋值没有作用)

  • 相关阅读:
    PBI REFRESH ERROR
    PTE
    Python
    Chromedriver下载地址
    python pip安装selenium时报错, ..has requirement websocket-client==0.48.0, but you'll have websocket -client 0.57.0 which is incoopatibale.
    在打开方式里只有wps,找不到默认的MS OFFICE办公PPT打开方式 解决方案
    POWER BI
    POWER BI
    [未解决]POWER BI
    POWER BI 生成日期表
  • 原文地址:https://www.cnblogs.com/nullcodeworld/p/8580823.html
Copyright © 2011-2022 走看看