Print multiple lines output in java without using a new line character

前端 未结 9 1910
轮回少年
轮回少年 2020-12-18 15:40

this is one of the interview question. I am supposed to print multiple lines of output on command line, without using the newline(\\n) character in java. I trie

相关标签:
9条回答
  • 2020-12-18 16:14

    Ok, now I think I understand your question. What about this?

    println(String.format("%d%n%d%n%d%n%d%n%d%n", 1, 2, 3, 4, 5));
    
    0 讨论(0)
  • 2020-12-18 16:14

    One way is this: Platform Independent

    final String EOL = System.getProperty("line.separator");
    System.out.println('1' + EOL + '2' + EOL + '3' + EOL + '4' + EOL + '5');
    

    This is Platform Dependent

    char eol = (char) 13;
    System.out.println("" + '1' + eol + '2' + eol + '3' + eol + '4');
    
    0 讨论(0)
  • 2020-12-18 16:14

    ANSI terminal escape codes can do the trick.

    Aside: Since System.out is a PrintStream, it may not be able to support the escape codes.

    However, you can define your own println(msg) function, and make one call to that. Might be cheating, but unless they explicitly say System.out.println, you're golden (hell, even if they do, you can define your own object named System in the local scope using a class defined outside your function, give it a field out with a function println(msg) and you're still scot-free).

    0 讨论(0)
提交回复
热议问题