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

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

    No loops, 1 println call, +flexibility:

    public static void main (String[] args) {
        print(5);
    }
    
    final String newLine = System.getProperty("line.separator");
    public void print(int fin) {
        System.out.println(printRec("",1,fin));
    }
    private String printRec(String s, int start, int fin) {
        if(start > fin)
            return s;
        s += start + newLine;
        return printRec(s, start+1, fin);
    }
    

提交回复
热议问题