System.err.print(ln)在同一行与system.out.println?JAVA

huangapple go评论71阅读模式
英文:

System.err.print(ln) on the same line as a system.out.println? JAVA

问题

我有一个命令行游戏需要为大学项目制作,(我知道这是不好的做法)我希望生命值以红色打印出来。我计划使用System.err.print来以红色打印心形符号,虽然这样可以实现,但是它们不会与System.out.print("")输出连接在一起。
这是否可能,或者我可以提供某种覆盖函数?

当使用以下代码时:
System.out.print("玩家生命数量:");
System.out.print("♥ ♥ ♥ ♥ ♥") 此时输出将会在同一行,如预期。
然而,当我将代码更改为:System.out.print("玩家生命数量:");
System.err.print("♥ ♥ ♥ ♥ ♥") 输出将会分成两行...

Netbeans IDE v12.0...
非常感谢您的任何帮助或想法!

谢谢!

英文:

I have a CLI game that I have to make for a uni project and (I know its bad practice) want the lives to be printed in red. I was planning on using the System.err.print to print the hearts in red which works, however they won't join onto my System.out.print("") output.
Is this possible or can I provide some sort of override func?

When using the code;
System.out.print("Player Lives: ");
System.out.print("♥ ♥ ♥ ♥ ♥") My output will be on the same line as expected. However, when I change the code to: System.out.print("Player Lives: ");
System.err.print("♥ ♥ ♥ ♥ ♥") the output will be split on to two seperate lines...

Netbeans IDE v12.0...
any help or ideas would be greatly appreciated!

Thank you!

答案1

得分: 2

这个问题的简单答案是:最好不要这么做!

  1. 当你将输出发送到 System.out 和 System.err 时,你无法控制输出在“控制台”上显示时如何交错。交错的方式将取决于许多因素,这些因素是 Java 程序无法控制的

  2. 你可能可以利用你的 IDE 在其控制台窗口中以红色显示 System.err 输出的“特性”,但你会发现在其他情境<sup>1</sup>中它不起作用。例如,在典型的命令 shell 中。

  3. 往往可以让控制台以不同的颜色输出文本。然而,这依赖于控制台是否支持(通常是)ANSI 转义码。这些转义码在最终用户的控制台 / 终端 / 终端模拟器上不一定启用。如果它们没有启用,用户在屏幕上会看到一堆奇怪的东西。

虽然有方法来处理这个问题(例如使用 Java 终端库),但总会存在问题。例如,如果你的讲师 / 助教运行你的应用程序,捕获输出并尝试在(比如)less 中查看它,他们可能会看到奇怪的东西而不是颜色。

我的建议是:除非特定的作业要求输出带颜色的文本(以及像“♥”这样的字符)...最好不要这么做。这会让你的作业更加困难,并在讲师 / 助教运行和评分代码的上下文中没有正确处理时导致分数丢失。

(你可能会认为这是一个有用的学习内容。但我的观点是,它比你想象的要没那么有用。如今,大多数应用程序使用 GUI 框架或 Web 浏览器来实现用户界面。基于控制台的界面通常被视为老式。)


<sup>1 - 我注意到你的评论,你的讲师希望获得整个 NetBeans 项目,并将在相同的 IDE 中运行代码。</sup>

英文:

The simple answer to this is: Just don't do it!

  1. When you send output to System.out and System.err, you have no control over how the output is going to interleave when displayed on a "console". The interleaving is going to depend on a number of factors that are outside of the control of a Java program.

  2. You may be able to make use of your IDE's "feature" of displaying System.err output in red on its console window, but you will find that it doesn't work in other contexts<sup>1</sup>. For example, from a typical command shell.

  3. It is often possible to get a console to output text in different colors. However this is relying on the console to support (typically) ANSI Escape Codes. These are not necessarily enabled on your end user's console / terminal / terminal emulator. If they are not enabled, the user gets a bunch of weird stuff on their screen.

There are ways to deal with this (e.g. using a Java terminal library), but there will always be rough edges. For example, if your Lecturer / TA runs your app, captures the output and tries to view it with (say) less, they are likely to see weird stuff rather than colors.

My advice: unless it is a specific assignment requirement to output colorized text (and dingbat characters like "♥") ... don't do it. You are making your assignment more difficult, and setting yourself for losing marks if you don't get it right in the context that the lecturer / ta uses to run and mark your code.

(You might argue that this is a useful thing to learn to do. My counter is that it is a lot less useful than you would think. Most applications these days use a GUI framework or a web browser to implement their user interface. Console based interfaces are typically viewed as old fashioned.)


<sup>1 - I note your comment that your lecturer is expecting to be given the entire NetBeans project, and will run the code in the same IDE.</sup>

答案2

得分: 2

如果心形图案不表示错误,请不要将它们打印在错误流上。

如果你想要以红色显示心形图案,这其实相当容易做到。你需要了解一些关于控制台的知识(不需要太多),以及在控制台上进行彩色打印的含义。

控制台是取代物理设备的程序。在旧时代,控制台类似于连接到通信通道的电视屏幕。这个电视屏幕也有一个键盘。控制台负责将信息发送给“计算机”,接收计算机的数据(控制台控制代码)并呈现那些信息。

发送和接收的大部分数据都是文本。今天,我们用程序模拟控制台,而“tty设备”则是通信通道的模拟(今天是一个设备文件,而不是调制解调器或串行线)。当你输入时,数据被写入连接到你的控制台的“tty”,当计算机显示文本时,控制台会读取tty并显示它所读取的任何内容。

之所以对于颜色很重要,是因为你需要在输出中打印控制台控制代码。第一个控制台控制代码是将控制台切换到显示红色文本,第二个控制台控制代码是将控制台切换到显示“正常”文本。通过进一步的研究,你可以轻松地显示16种不同的颜色,结合16种不同的背景颜色,闪烁文本等。一些控制台支持更多的“模式”操作,你可以尝试你的控制台。有一套标准的模式,几乎所有的控制台都广泛支持,称为ANSI模式。

控制台的默认信息是文本,然后以文本形式显示出来。要想让控制台接受一个命令,它将不会是普通文本。

public static final String RED = "\u001B[31m";
public static final String RESET = "\u001B[0m";

第一个字符串将控制台切换为红色文本,第二个字符串将颜色模式重置为默认。

因此,要打印一个红色的“hello”,你可以这样做:

System.out.println(RED + "hello" + RESET);

你还可以创建一个函数来打印红色的内容:

public void printRed(String message) {
   System.out.println(RED + message + RESET);
}

或者创建一个函数来构建一个“包装好”的红色字符串,等等。

英文:

If the hearts don't represent an error, don't print them on an error stream.

You want the hearts in red, well that is pretty easy to do. You need to learn a bit about consoles (not too much) and what it means to print in color on a console.

Consoles are programs that replaced phyiscal devices. In the old days, a console was something like a television screen hooked up to a communications channel. This TV screen had a keyboard too. The console was responsible for sending information to the "computer" and for receiving data from the computer (console control codes) and presenting that information.

Most of the data sent and received was text. Today, we simulate the console with a program, and the "tty device" is the simulation of the communications channel (a device file today, instead of a modem or serial line). When you type, the data is written to the "tty" attached to your console, and when the computer displays text, the console reads the tty and shows whatever it read.

The reason this is important for color is because you need to print console control codes in your output. The first console control code is to shift the console into displaying red text, the second is to shift the console into displaying "normal" text. With a little more research, you can display (easily) 16 different colors, combined with 16 different background colors, flashing text, etc. Some consoles support more "modes" of operation, you can experiment with yours. There is a standard set of modes, widely supported by nearly all consoles, called ANSI modes.

The default information to a console is text, which is then displayed as text. To get a console to accept a command, it will not be plain text.

public static final String RED = &quot;\u001B[31m&quot;; 
public static final String RESET = &quot;\u001B[0m&quot;;

the first string will shift the console into red text, the second will reset the color mode to default.

So to print a red "hello" you would then do:

System.out.println(RED + &quot;hello&quot; + RESET);

And you could make a function to print red stuff

public void printRed(String message) {
   System.out.println(RED + message + RESET);
}

or a function to build a "wrapped" red string, etc.

huangapple
  • 本文由 发表于 2020年8月30日 10:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定