• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Famio
V2EX  ›  程序员

看到的一个题,随手分享一下

  •  
  •   Famio · Feb 10, 2017 · 4142 views
    This topic created in 3554 days ago, the information mentioned may be changed or developed.
    Supplement 1  ·  Feb 10, 2017
    #1 是正解。但是只是方法其一。还有一种可以使 println 也顺理成章而不在 method 中直接结束程序的办法。看看各位还有没有其它路能走。
    21 replies  •  2017-02-11 23:16:41 +08:00
    ArthurKing
    ArthurKing
    Feb 10, 2017❤️9
    1
    static void method(int a,int b){
    System.out.println("a=100,b=200");
    //throw new NullPointerException();
    System.exit(0);
    }
    finalspeed
    finalspeed
    Feb 10, 2017
    2
    楼上的 66666....
    kaka8wp
    kaka8wp
    Feb 10, 2017
    3
    楼上的 66666....
    fighter2011
    fighter2011
    Feb 10, 2017
    4
    等待其他解答
    ininit
    ininit
    Feb 10, 2017 via Android
    5
    楼上的 666
    qiayue
    qiayue
    Feb 10, 2017
    6
    @ArthurKing exit 哈哈哈
    bianhua
    bianhua
    Feb 10, 2017❤️1
    7
    跟你们说吧,只有 1 楼的答案是对的,因为后面那两个它么是的 println (笑哭
    ArthurKing
    ArthurKing
    Feb 10, 2017
    8
    @bianhua 不不不,还可以换成 while(true); ( ˙-˙ )
    happyyl
    happyyl
    Feb 10, 2017 via Android
    9
    楼上的 66666....
    Famio
    Famio
    OP
    Feb 10, 2017
    10
    @bianhua 哈哈哈,大兄弟是的。
    jasontse
    jasontse
    Feb 10, 2017 via iPad
    11
    套路。。。
    haozibi
    haozibi
    Feb 10, 2017
    12
    1 楼真是简单粗暴
    vh2h
    vh2h
    Feb 10, 2017
    13
    自古一楼出奇迹,看来没错啊!膜拜一楼。
    aitaii
    aitaii
    Feb 10, 2017 via Android
    14
    正常的话是 a
    b
    aristotll
    aristotll
    Feb 10, 2017
    15
    第二种方法是啥 第一种我也想到了...
    valkyrja
    valkyrja
    Feb 10, 2017 via Android
    17
    似乎可以覆盖 system.out.println 然后 replace?
    padeoe
    padeoe
    Feb 11, 2017❤️1
    18
    长知识,帮补代码:
    public static void method(int a, int b) {
    System.setOut(new PrintStream(System.out) {
    @Override
    public void println(String s) {
    super.println(s.equals("a=10") ? "a=100" : "b=200");
    }
    });
    }
    lxy42
    lxy42
    Feb 11, 2017 via Android
    19
    在 method 中打印结果后,将 stdout 重定向到 stderr
    zhilincom
    zhilincom
    Feb 11, 2017
    20
    @padeoe 应该是 super.print(s.equals("a=10") ? "a=100," : "b=200");
    flyingghost
    flyingghost
    Feb 11, 2017
    21
    ```
    public static void method(int a, int b){
    final PrintStream oldOut = System.out;
    PrintStream ps=new PrintStream(new FilterOutputStream(oldOut) {
    boolean meetOne = false;
    @Override
    public void write(int b) throws IOException {
    if( b =='0'){
    oldOut.write('0');
    oldOut.write(b);
    }
    else if(b == '1'){
    if(!meetOne){
    meetOne = true;
    oldOut.write(b);
    }
    else {
    oldOut.write('2');
    }
    }
    else {
    oldOut.write(b);
    }
    }
    });
    System.setOut(ps);
    }
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5214 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 67ms · UTC 01:27 · PVG 09:27 · LAX 17:27 · JFK 20:27
    ♥ Do have faith in what you're doing.