How can I move a cursor up the lines in a terminal

2020-07-27 06:49发布

问题:

I'm trying to print 2 side-by-side figures of the form:

@@@ @@@

@@@ @@@

@@@ @@@

After printing the first square, how can I move my cursor back to the top line, to start drawing my second square?

回答1:

The println statement essentially just sends characters to System.out and it is up to the tool you use to see these characters with, to do what you want to do.

For terminals (CMD.EXE/Terminal.app/etc) the convention is - as with printers - to have special sequences not corresponding to characters seen, but instead invoke some action. The historical tradition is to use so-called vt100 sequences, which terminals under OS X and Linux usually understand, but that CMD.EXE doesn't. If so, you can use an ncurses library to do what you want.

If not, a typical approach is to build the whole picture you want to print internally in a double array before printing it, and then just print it as you normally do.



标签: java loops