Java - New Line Character Issue

后端 未结 4 851
半阙折子戏
半阙折子戏 2021-01-28 01:50

I have a code like,

String str = \" \" ; 

while(  cond ) {

  str = str + \"\\n\" ;

}

Now, I don\'t know why at the time of printing, the out

4条回答
  •  长发绾君心
    2021-01-28 02:44

    The newline character is considered a control character, which doesn't print a special character to the screen by default.

    As an example, try this:

    String str = "Hi";
    while (cond) {
        str += "\n"; // Syntactically equivalent to your code
    }
    str += "Bye";
    System.out.println(str);
    

提交回复
热议问题