简体   繁体   English

我想知道 system.out.println() 在这段代码中做了什么

[英]I want to know what is system.out.println() doing in this code

I don't know why there are system.out.println in the end of these code.我不知道为什么这些代码的末尾有system.out.println。 what is this and why is it here?这是什么,为什么在这里?

This code is a code from my friend, he told me to understand this code and I don't understand why there are system.out.println after this code.这段代码是我朋友的一段代码,他告诉我要理解这段代码,我不明白为什么这段代码后面还有system.out.println。

public class Nestedlooplab2 {
    public static void main(String[] args) {
        for (byte i = 1; i <= 5; i++) {
            for (byte j = 1; j <= i; j++) {
                System.out.print(".");
            }
            for (byte k=1; k<=(5-i);k++) {
                System.out.print("*");
            } System.out.println();}
    }
    }

Welcome to stack overflow!欢迎来到堆栈溢出!

By itself a System.out.println() will just print a newline character, eg \n . System.out.println()本身只会打印一个换行符,例如\n If you were to add a parameter to this statement it would print your parameter along with a newline.如果您要向此语句添加参数,它将打印您的参数和换行符。

Here are the JavaDocs for println 这是println的 JavaDocs

Terminates the current line by writing the line separator string.通过写入行分隔符字符串来终止当前行。 The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').行分隔符字符串由系统属性 line.separator 定义,不一定是单个换行符 ('\n')。

The code you linked would print a certain amount of .您链接的代码将打印一定数量的. characters and certain amount of * characters without any line breaks.字符和一定数量的*字符,没有任何换行符。 The final System.out.println();最后的System.out.println(); would then print a newline character and the loop would start over again.然后将打印一个换行符,循环将重新开始。

Output: Output:

.****
..***
...**
....*
.....

The output of your code is:您的代码的 output 是:

.****
..***
...**
....*
.....

Without the System.out.println();没有System.out.println(); , the output would be: ,output 将是:

.****..***...**....*.....

The System.out.println(); System.out.println(); prints a new line character to the screen for each interaction of the loop.为循环的每次交互在屏幕上打印一个换行符。

First, let's take a look at the output of your code:首先,让我们看一下您的代码的output:

.****
..***
...**
....*
.....

As a String, this is the same as:作为字符串,这与以下内容相同:

.****\n..***\n...**\n....*\n.....\n

Using System.out in Java writes to the standard output-stream of characters by default.默认情况下,在 Java 中使用System.out会写入字符的标准输出流。 When you write to this stream, you can use various methods, including System.out.print and System.out.println .当你写这个 stream 时,你可以使用各种方法,包括System.out.printSystem.out.println Calling print will output the exact String that you provide to it, whereas calling println will output the String you provide to it, followed by a new line (the line separator for your system).调用print将 output 您提供给它的确切字符串,而调用println将 output 您提供给它的字符串,后跟一个新行(系统的行分隔符)。 If you call System.out.println() ( println with no parameter), you will output the String you provided ( "" ) as well as move the output to the next line.如果您调用System.out.println() ( println没有参数),您将 output 您提供的字符串 ( "" ) 并将 output 移动到下一行。 Essentially, this means removing calls to System.out.println() in your code will result in the following output:本质上,这意味着在您的代码中删除对System.out.println()的调用将导致以下 output:

.****..***...**....*.....

This output will look exactly the same as a String.这个 output 看起来与字符串完全相同。 As you can see, there are no newline characters ( \n ) within your output when you only call System.out.print and don't call System.out.println .如您所见,当您仅调用System.out.print而不调用System.out.println时,output 中没有换行符( \n )。

Finally, let's take a look at your code in the context of making it easier to read and understand.最后,让我们在更易于阅读和理解的上下文中查看您的代码。 I'm using the Java 11+ feature String.repeat to massively simplify the operation of repeating a String here:我在这里使用 Java 11+ 功能String.repeat来大量简化重复字符串的操作:

public static void main(String[] args) {
  for (int i = 1; i <= 5; i++) {
    System.out.println(".".repeat(i)+"*".repeat(5-i));
  }
}

Output: Output:

.****
..***
...**
....*
.....

This is equivalent to the original output.这相当于原来的output。 However, it's much clearer to read and understand what is going on.但是,阅读和理解正在发生的事情要清楚得多 Supposing that you don't have access to those features, you can do the following instead:假设您无权访问这些功能,则可以改为执行以下操作:

for (int i = 1; i <= 5; i++) {
  for(int rpts = 0; rpts < i; rpts++) {
    System.out.print('.');
  }
  for(int rpts = i; rpts < 5; rpts++) {
    System.out.print('*');
  }
  System.out.println();
}

This snippet of code also has the same output.这段代码也有相同的 output。 Its content is not much different from your code snippet, as your code snippet does have the right idea.它的内容与您的代码片段没有太大区别,因为您的代码片段确实有正确的想法。 However, it is more consistently formatted, which makes it easier for yourself and others to read the code.但是,它的格式更一致,这使您和其他人更容易阅读代码。 Do note how the repeats are labelled with a name rpts , and (in both examples) the iteration variables are ints.请注意重复是如何用名称rpts标记的,并且(在两个示例中)迭代变量都是整数。 Java programmers usually use ints to iterate because the space you save from using bytes to iterate is negligible enough to ignore for most applications, and integers cover most ranges of values you might want to iterate over. Java 程序员通常使用整数进行迭代,因为使用bytes进行迭代节省的空间对于大多数应用程序来说可以忽略不计,并且整数覆盖了您可能想要迭代的大部分值范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM