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

前端 未结 9 1908
轮回少年
轮回少年 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:07

    You can do it recursively:

    public void foo(int currNum) {
      if (currNum > 5) 
        return;
      println(currNum);
      foo(currNum + 1);
    }
    

    Then you are only using a single println and you aren't using a for or while loop.

提交回复
热议问题